1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
//! Structs and methods for Bolin & Rodhe reservoir models.
use crate::errors;
use crate::plot;
use crate::utils;
use log::*;
use rand::{Rng, SeedableRng};
use rand_distr::{Distribution, Exp};
use rayon::prelude::*;
use realfft::RealFftPlanner;
use rustfft::num_complex::Complex;
use serde::{Deserialize, Serialize};

/// Holder struct for passing test values between functions.
/// Makes functions calls and returns less confusing.
#[derive(Clone, Debug)]
pub struct Fit {
    ad: f64,
    ada: f64,
    chs: f64,
    kp: f64,
    ks: f64,
    ksa: f64,
}

/// Summary statistics for several Fit objects.
#[derive(Clone, Debug)]
pub struct Fits {
    fit: Fit,
    n: f64,
}

/// Holder struct for goodness-of-fit statistics.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Gof {
    input: f64,
    output: f64,
    ad: f64,
    ada: f64,
    chs: f64,
    kp: f64,
    ks: f64,
    ksa: f64,
    n: f64,
}

impl Gof {
    /// Convert csv record to Gof struct.
    pub fn read(path: &str) -> Result<Vec<Gof>, errors::ResError> {
        let mut gof = Vec::new();
        let var = std::fs::File::open(path)?;
        let mut rdr = csv::Reader::from_reader(var);
        for result in rdr.records() {
            let row = result?;
            let row: Gof = row.deserialize(None)?;
            gof.push(row);
        }
        Ok(gof)
    }

    /// Write statistical results to csv file.
    pub fn record(rec: &mut Vec<Gof>, title: &str) -> Result<(), errors::ResError> {
        let mut wtr = csv::Writer::from_path(title)?;
        for i in rec {
            wtr.serialize(i)?;
        }
        wtr.flush()?;
        Ok(())
    }

    /// Get the values associated with fields of the struct.
    pub fn values(&self) -> (f64, f64, f64, f64, f64, f64, f64, f64, f64) {
        (
            self.input,
            self.output,
            self.ad,
            self.ada,
            self.chs,
            self.kp,
            self.ks,
            self.ksa,
            self.n,
        )
    }
}

/// Holder struct for goodness-of-fit statistics.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ReservoirFit {
    rate: f64,
    period: f64,
    ad: f64,
    ch: f64,
    kp: f64,
    ks: f64,
    n: f64,
}

impl ReservoirFit {
    /// Convert csv record to Gof struct.
    pub fn read(path: &str) -> Result<Vec<ReservoirFit>, errors::ResError> {
        let mut fit = Vec::new();
        let var = std::fs::File::open(path)?;
        let mut rdr = csv::Reader::from_reader(var);
        for result in rdr.records() {
            let row = result?;
            let row: ReservoirFit = row.deserialize(None)?;
            fit.push(row);
        }
        Ok(fit)
    }

    /// Write statistical results to csv file.
    pub fn record(rec: &mut Vec<ReservoirFit>, title: &str) -> Result<(), errors::ResError> {
        let mut wtr = csv::Writer::from_path(title)?;
        for i in rec {
            wtr.serialize(i)?;
        }
        wtr.flush()?;
        Ok(())
    }

    /// Get the values associated with fields of the struct.
    pub fn values(&self) -> (f64, f64, f64, f64, f64, f64, f64) {
        (
            self.rate,
            self.period,
            self.ad,
            self.ch,
            self.kp,
            self.ks,
            self.n,
        )
    }
}

/// Holds parameters for bootstrapping confidence intervals of reservoir models.
#[derive(Clone, Debug)]
pub struct Bootstrap {
    model: Model,
    bins: usize,
    samples: usize,
}

impl Bootstrap {
    /// Sets the model bin number for comparing ranges in [search](#method.search).
    pub fn bins(mut self, size: usize) -> Self {
        self.bins = size;
        self
    }

    /// Creates a Bootstrap struct from a given Model.
    pub fn new(model: Model) -> Self {
        Bootstrap {
            model,
            bins: 100,
            samples: 3,
        }
    }

    /// Sets the number of samples desired for calculating the mean in a range,
    /// called by [search](#method.search).
    pub fn samples(mut self, count: usize) -> Self {
        self.samples = count;
        self
    }

    /// Fits range of rates to an observed distribution using a steady state reservoir.
    pub fn search(
        &mut self,
        rate: std::ops::Range<f64>,
        obs: &[f64],
        path: &str,
    ) -> Result<(), errors::ResError> {
        // initialize time variables
        let dur = std::time::Duration::new(60 * 60 * self.model.duration, 0);
        let now = std::time::SystemTime::now();
        let mut rec = Vec::new();
        // load existing record if any
        let exists = std::path::Path::new(path).exists();
        match exists {
            true => {
                let mut fits: Vec<f64> = utils::read_f64(path)?;
                rec.append(&mut fits);
            }
            false => {}
        }
        // perform bootstrap
        let mut count: usize = 0;
        let rec_path = format!("{}{}", path, "rec.csv");
        while std::time::SystemTime::now() < now + dur {
            let mut gof = Vec::new();
            let boot = utils::bootstrap(obs);
            while gof.len() < (self.bins * self.samples) {
                let mut new = self.model.steady(rate.clone(), &boot);
                gof.append(&mut new);
                println!(
                    "{}% complete.",
                    (gof.len() as f64 / (self.bins * self.samples) as f64 * 100.0).round()
                );
            }
            let gof_path = format!("{}{}{}{}", path, "gof_", count, ".csv");
            let png_path = format!("{}{}{}{}", path, "gof_", count, ".png");
            utils::record(&mut gof, &gof_path)?;
            count += 1;
            let (x, y) = Record::bin_ave(&gof, self.bins);
            plot::xy(&x, &y, &png_path)?;
            rec.push(utils::low_point(&x, &y));
            utils::record(&mut rec, &rec_path)?;
        }

        Ok(())
    }
}

/// Holds model characteristics associated with a reservoir.
#[derive(Clone, Debug)]
pub struct Model {
    reservoir: Reservoir,
    period: f64,
    runs: usize,
    batch: usize,
    duration: u64,
    range: i32,
}

impl Model {
    /// Sets the model batch size to desired number of samples.
    pub fn batch(mut self, samples: usize) -> Self {
        self.batch = samples;
        self
    }

    /// Sets the model period to desired time in years.
    pub fn duration(mut self, hours: &u64) -> Self {
        self.duration = hours.to_owned();
        self
    }

    /// Fits a range of input and output rates to an observed accumulation record.
    /// A wrapper that calls [fit_rng](struct.Reservoir.html#method.fit_rng) runs for a specified period of time `dur`,
    /// and writes the results to a csv file.
    ///  - `self` - set inherited age before running.
    ///  - `input` is the range of possible input rates.
    ///  - `output` is the range of possible output rates.
    ///  - `obs` is the vector of observed values in years.
    ///  - `title` labels the csv file, and must be a valid path name ending in ".csv"
    ///  - Returns a csv written to the path specified by `title` containing goodness-of-fit statistics.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use reservoirs::prelude::*;
    ///
    /// fn main() -> Result<(), ResError> {
    /// // mean expected deposit age and inherited age by facies
    /// let dep = Sample::read("https://github.com/crumplecup/reservoirs/blob/master/examples/dep.csv")?;
    /// let iat = Sample::read("https://github.com/crumplecup/reservoirs/blob/master/examples/iat.csv")?;
    ///
    /// // subset mean ages of debris flows
    /// let df: Vec<f64> = dep.iter()
    ///     .filter(|x| x.facies == "DF")
    ///     .map(|x| x.age)
    ///     .collect();
    ///
    /// // subset inherited ages
    /// let ia: Vec<f64> = iat.iter()
    ///     .map(|x| x.age)
    ///     .collect();
    ///
    /// let mut debris_flows = Reservoir::new()
    ///     .input(&0.687)?
    ///     .output(&0.687)?
    ///     .inherit(&ia);
    ///
    /// // model parameters
    /// let batch = 10;  // fit 10 input/output pairs at a time using rayon
    /// let duration = 1; // fit model for 1 hour
    /// let period = 30000.0; // run simulations for 30000 years
    /// let runs = 1000; // run 1000 simulated accumulations per candidate pair for goodness-of-fit
    ///
    /// // create reservoir model using builder pattern
    /// let mut model = Model::new(debris_flows)
    ///     .batch(batch)
    ///     .duration(&duration)
    ///     .period(&period)
    ///     .runs(runs);
    /// // for one hour, fit batches of 10 randomly selected rate pairs (from range 0.01 to 1.0)
    /// // to observed debris flows
    /// // by running 1000 simulations for 30000 years for each pair
    /// model.fit_range(0.01..1.0, 0.01..1.0, &df, "examples/df_fit_1k.csv");
    /// Ok(())
    /// }
    /// ```
    pub fn fit_range(
        &mut self,
        input: std::ops::Range<f64>,
        output: std::ops::Range<f64>,
        obs: &[f64],
        title: &str,
    ) {
        let dur = std::time::Duration::new(60 * 60 * self.duration, 0);
        let now = std::time::SystemTime::now();
        let mut rec = Vec::new();
        let exists = std::path::Path::new(title).exists();
        match exists {
            true => {
                let mut gof: Vec<Gof> = Gof::read(title).unwrap();
                rec.append(&mut gof);
            }
            false => {}
        }
        while std::time::SystemTime::now() < now + dur {
            let mut new = self.fit_rng(input.clone(), output.clone(), obs);
            {
                rec.append(&mut new);
            }
            Gof::record(&mut rec, title).unwrap();
        }
    }

    /// Randomly selects rate pairs from ranges `input` and `output`, and simulates accumulation records
    /// in batches using [fit_rate](#method.fit_rate).  Returns the selected input/output pair and the mean
    /// goodness-of-fit statistics for each pair.  Called by [fit_range](method.fit_range).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     let ages = vec![10.0, 42.0, 77.7, 12.0, 99.9, 10000.0, 777.7];
    ///
    ///     let mut debris_flows = Reservoir::new()
    ///         .input(&0.862)?
    ///         .output(&0.862)?;
    ///
    ///     // model parameters
    ///     let batch = 10;  // fit 10 input/output pairs at a time using rayon
    ///     let period = 3000.0; // run simulations for 3000 years
    ///     let runs = 20; // run 20 simulated accumulations per candidate pair for goodness-of-fit
    ///
    ///     // create reservoir model using builder pattern
    ///     let mut model = Model::new(debris_flows)
    ///         .batch(batch)
    ///         .period(&period)
    ///         .runs(runs);
    ///     // fit batches of 10 randomly selected rate pairs (from range 0.01 to 1.0)
    ///     // to observed debris flows
    ///     // by running 20 simulations for 3000 years for each pair
    ///     let gofs = model.fit_rng(0.01..1.0, 0.01..1.0, &ages);
    ///     Ok(())
    /// }
    /// ```
    pub fn fit_rng(
        &mut self,
        input: std::ops::Range<f64>,
        output: std::ops::Range<f64>,
        obs: &[f64],
    ) -> Vec<Gof> {
        let mut inputs = Vec::with_capacity(self.batch);
        let mut outputs = Vec::with_capacity(self.batch);
        let mut fits = Vec::with_capacity(self.batch);
        for i in 0..self.batch {
            inputs.push(
                rand::distributions::Uniform::from(input.clone()).sample(&mut self.reservoir.range),
            );
            outputs.push(
                rand::distributions::Uniform::from(
                    output.clone().start.max(inputs[i] * 0.975)
                        ..output.clone().end.min(inputs[i] * 1.0125),
                )
                .sample(&mut self.reservoir.range),
            );
            fits.push(
                self.clone().reservoir(
                    self.reservoir
                        .clone()
                        .input(&inputs[i])
                        .unwrap()
                        .output(&outputs[i])
                        .unwrap(),
                ),
            );
        }
        let gof: Vec<Fits> = fits.par_iter().map(|x| x.clone().fit_rate(obs)).collect();
        let mut gofs = Vec::with_capacity(self.batch);
        for i in 0..self.batch {
            gofs.push(Gof {
                input: inputs[i],
                output: outputs[i],
                ad: gof[i].fit.ad,
                ada: gof[i].fit.ada,
                chs: gof[i].fit.chs,
                kp: gof[i].fit.kp,
                ks: gof[i].fit.ks,
                ksa: gof[i].fit.ksa,
                n: gof[i].n,
            })
        }
        gofs
    }

    /// Runs simulations on a reservoir,
    /// returns the mean goodness-of-fit statistics compared to accumulation record `other`.
    /// Called by [fit_rng](#method.fit_rng) and [steady](#method.steady).  To use,
    /// set characteristics of the reservoir and model before running.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     let ages = vec![10.0, 42.0, 77.7, 12.0, 99.9, 10000.0, 777.7];
    ///
    ///     let mut debris_flows = Reservoir::new()
    ///         .input(&0.862)?
    ///         .output(&0.862)?;
    ///
    ///     // model parameters
    ///     let period = 3000.0; // run simulations for 3000 years
    ///     let runs = 20; // run 20 simulated accumulations per candidate pair for goodness-of-fit
    ///
    ///     // create reservoir model using builder pattern
    ///     let mut model = Model::new(debris_flows)
    ///         .period(&period)
    ///         .runs(runs);
    ///     // fit selected rate pairs
    ///     // to observed debris flows
    ///     // by running 20 simulations for 3000 years for each pair
    ///     let fits = model.fit_rate(&ages);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub fn fit_rate(&mut self, other: &[f64]) -> Fits {
        let mut res: Vec<Reservoir> = Vec::with_capacity(self.runs);
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        let seeds: Vec<u64> = seeder
            .sample_iter(&mut self.reservoir.range)
            .take(self.runs)
            .collect();
        for seed in seeds {
            res.push(self.reservoir.clone().range(seed));
        }
        res = res.par_iter().cloned().map(|x| x.sim()).collect();
        let fits: Vec<Fit> = res.par_iter().cloned().map(|x| x.gof1(other)).collect();
        let ads: Vec<f64> = fits.iter().map(|x| x.ad).collect();
        let ad = utils::median(&ads);
        let adas: Vec<f64> = fits.iter().map(|x| x.ada).collect();
        let ada = utils::median(&adas);
        let chss: Vec<f64> = fits.iter().map(|x| x.chs).collect();
        let chs = utils::median(&chss);
        let kps: Vec<f64> = fits.iter().map(|x| x.kp).collect();
        let kp = utils::median(&kps);
        let kss: Vec<f64> = fits.iter().map(|x| x.ks).collect();
        let ks = utils::median(&kss);
        let ksas: Vec<f64> = fits.iter().map(|x| x.ksa).collect();
        let ksa = utils::median(&ksas);
        let ns: Vec<f64> = res.iter().map(|x| x.mass.len() as f64).collect();
        let n = utils::median(&ns);

        Fits {
            fit: Fit {
                ad,
                ada,
                chs,
                kp,
                ks,
                ksa,
            },
            n,
        }
    }

    /// Fits a range of steady state reservoirs to an observed accumulation record.
    /// A wrapper that calls [steady](struct.Reservoir.html#method.steady) runs for a specified period of time `dur`,
    /// and writes the results to a csv file.
    ///  - `self` - set inherited age before running.
    ///  - `rate` is the range of possible rates.
    ///  - `obs` is the vector of observed values in years.
    ///  - `title` labels the csv file, and must be a valid path name ending in ".csv"
    ///  - Returns a csv written to the path specified by `title` containing goodness-of-fit statistics.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///
    /// // mean expected deposit age and inherited age by facies
    /// let dep = Sample::read("https://github.com/crumplecup/reservoirs/blob/master/examples/dep.csv")?;
    /// let iat = Sample::read("https://github.com/crumplecup/reservoirs/blob/master/examples/iat.csv")?;
    ///
    /// // subset mean ages of debris flows
    /// let df: Vec<f64> = dep.iter()
    ///     .filter(|x| x.facies == "DF")
    ///     .map(|x| x.age)
    ///     .collect();
    ///
    /// // subset inherited ages
    /// let ia: Vec<f64> = iat.iter()
    ///     .map(|x| x.age)
    ///     .collect();
    ///
    /// let mut debris_flows = Reservoir::new()
    ///     .input(&0.687)?
    ///     .output(&0.687)?
    ///     .inherit(&ia);
    ///
    /// // model parameters
    /// let batch = 10;  // fit 10 input/output pairs at a time using rayon
    /// let duration = 1; // fit model for 1 hour
    /// let period = 30000.0; // run simulations for 30000 years
    /// let runs = 1000; // run 1000 simulated accumulations per candidate pair for goodness-of-fit
    ///
    /// // create reservoir model using builder pattern
    /// let mut model = Model::new(debris_flows)
    ///     .batch(batch)
    ///     .duration(&duration)
    ///     .period(&period)
    ///     .runs(runs);
    /// // for one hour, fit batches of 10 randomly selected rate pairs (from range 0.01 to 1.0)
    /// // to observed debris flows
    /// // by running 1000 simulations for 30000 years for each pair
    /// model.fit_steady(0.01..1.0, &df, "examples/df_fit_1k.csv");
    ///
    /// Ok(())
    /// }
    /// ```
    pub fn fit_steady(&mut self, rate: std::ops::Range<f64>, obs: &[f64], title: &str) {
        let dur = std::time::Duration::new(60 * 60 * self.duration, 0);
        let now = std::time::SystemTime::now();
        let mut rec = Vec::new();
        let exists = std::path::Path::new(title).exists();
        match exists {
            true => {
                let mut gof: Vec<Gof> = Gof::read(title).unwrap();
                rec.append(&mut gof);
            }
            false => {}
        }
        while std::time::SystemTime::now() < now + dur {
            let mut new = self.steady(rate.clone(), obs);
            {
                rec.append(&mut new);
            }
            Gof::record(&mut rec, title).unwrap();
        }
    }

    /// Return quantile statistics for transit times at randomly selected rates for a given duration.
    /// Calls [transits](#method.transits).
    pub fn fit_transits(
        &mut self,
        rate: std::ops::Range<f64>,
        title: &str,
    ) -> Result<(), errors::ResError> {
        let dur = std::time::Duration::new(60 * 60 * self.duration, 0);
        let now = std::time::SystemTime::now();
        let mut rec = Vec::new();
        let exists = std::path::Path::new(title).exists();
        match exists {
            true => {
                let mut stats: Vec<Transits> = Transits::read(title)?;
                rec.append(&mut stats);
            }
            false => {}
        }
        while std::time::SystemTime::now() < now + dur {
            let mut new = self.transits(rate.clone());
            {
                rec.append(&mut new);
            }
            utils::record(&mut rec, title)?;
        }
        Ok(())
    }

    /// Return weighted mean transit time of particles in the reservoir.
    ///
    /// # Examples
    ///
    /// ```{rust}
    /// use::reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     // model parameters
    ///     let batch = 10;  // fit 10 input/output pairs at a time using rayon
    ///     let period = 3000.0; // run simulations for 3000 years
    ///     let runs = 10; // run 10 simulated accumulations per candidate pair for goodness-of-fit
    ///
    ///     // create reservoir model using builder pattern
    ///     let mut model = Model::new(Reservoir::new().input(&0.73)?.output(&0.73)?)
    ///         .batch(batch)
    ///         .period(&period)
    ///         .runs(runs)
    ///         .range(period as i32);
    ///
    ///     let transits = model.fit_transit_time();
    ///     Ok(())
    /// }
    /// ```
    pub fn fit_transit_time(&mut self) -> f64 {
        let pmf = self.clone().transit_times();
        let index = utils::index_f64(0.0, pmf.len() as f64, 1.0);
        let wgt_mn = index
            .iter()
            .zip(pmf.iter())
            .map(|(a, b)| a * b)
            .collect::<Vec<f64>>();
        let mean = wgt_mn.iter().fold(0.0, |acc, x| acc + x);
        mean
    }

    /// Returns quantile statistics on transit times for rates in a given range.
    /// Calls [fit_transit_rate](#method.fit_transit_rate).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     // model parameters
    ///     let batch = 10;  // fit 10 input/output pairs at a time using rayon
    ///     let period = 3000.0; // run simulations for 3000 years
    ///     let runs = 10; // run 10 simulated accumulations per candidate pair for goodness-of-fit
    ///
    ///     // create reservoir model using builder pattern
    ///     let mut model = Model::new(Reservoir::new().input(&0.73)?.output(&0.73)?)
    ///         .batch(batch)
    ///         .period(&period)
    ///         .runs(runs)
    ///         .range(period as i32);
    ///
    ///     let transits = model.transits((0.01..1.5));
    ///     Ok(())
    /// }
    /// ```
    pub fn transits(&mut self, rate: std::ops::Range<f64>) -> Vec<Transits> {
        info!("Randomly select rates from the given rates to fit.");
        let mut rates = Vec::with_capacity(self.batch);
        info!("Clone the parent model and give each copy a different reservoir rate.");
        let mut fits = Vec::with_capacity(self.batch);
        for i in 0..self.batch {
            rates.push(
                rand::distributions::Uniform::from(rate.clone()).sample(&mut self.reservoir.range),
            );
            fits.push(
                self.clone().reservoir(
                    self.reservoir
                        .clone()
                        .input(&rates[i])
                        .unwrap()
                        .output(&rates[i])
                        .unwrap(),
                ),
            );
        }
        let stats: Vec<f64> = fits.iter().map(|x| x.clone().fit_transit_time()).collect();
        let transits: Vec<Transits> = stats
            .iter()
            .zip(rates.iter())
            .map(|(a, b)| Transits { rate: *a, mean: *b })
            .collect();
        transits
    }

    /// Creates a new model from a given `reservoir` with default `period`, `runs` and `batch` values.
    pub fn new(reservoir: Reservoir) -> Self {
        Model {
            reservoir,
            period: 30000.0,
            runs: 100,
            batch: 10,
            duration: 1,
            range: 10000,
        }
    }

    /// Sets the model period to desired time in years.
    pub fn period(mut self, years: &f64) -> Self {
        self.period = years.to_owned();
        self
    }

    /// Sets the number of runs per sample to estimate goodness-of-fit.
    pub fn range(mut self, limit: i32) -> Self {
        self.range = limit;
        self
    }

    /// Sets the model reservoir.
    pub fn reservoir(mut self, res: Reservoir) -> Self {
        self.reservoir = res;
        self
    }

    /// Sets the number of runs per sample to estimate goodness-of-fit.
    pub fn runs(mut self, times: usize) -> Self {
        self.runs = times;
        self
    }

    /// Randomly selects a rate from ranges `rate` for a steady state reservoir,
    /// and simulates accumulation records
    /// in batches using [fit_rate](#method.fit_rate).  Returns the selected input/output pair and the mean
    /// goodness-of-fit statistics compared to `obs` for each pair.
    /// Called by [fit_steady](#method.fit_steady).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     let ages = vec![10.0, 42.0, 77.7, 12.0, 99.9, 10000.0, 777.7];
    ///     let mut debris_flows = Reservoir::new()
    ///         .input(&0.862)?
    ///         .output(&0.862)?;
    ///
    ///     // model parameters
    ///     let batch = 10;  // fit 10 input/output pairs at a time using rayon
    ///     let period = 3000.0; // run simulations for 30000 years
    ///     let runs = 20; // run 1000 simulated accumulations per candidate pair for goodness-of-fit
    ///
    ///     // create reservoir model using builder pattern
    ///     let mut model = Model::new(debris_flows)
    ///         .batch(batch)
    ///         .period(&period)
    ///         .runs(runs);
    ///     // fit a batch of 10 randomly selected rate pairs (from range 0.01 to 1.0)
    ///     // to observed debris flows
    ///     // by running 20 simulations for 3000 years for each pair
    ///     let gofs = model.steady(0.01..1.0, &ages);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub fn steady(&mut self, rate: std::ops::Range<f64>, obs: &[f64]) -> Vec<Gof> {
        info!("Randomly select rates from the given rates to fit.");
        let mut rates = Vec::with_capacity(self.batch);
        info!("Clone the parent model and give each copy a different reservoir rate.");
        let mut fits = Vec::with_capacity(self.batch);
        for i in 0..self.batch {
            rates.push(
                rand::distributions::Uniform::from(rate.clone()).sample(&mut self.reservoir.range),
            );
            fits.push(
                self.clone().reservoir(
                    self.reservoir
                        .clone()
                        .input(&rates[i])
                        .unwrap()
                        .output(&rates[i])
                        .unwrap(),
                ),
            );
        }
        let gof: Vec<Fits> = fits.par_iter().map(|x| x.clone().fit_rate(obs)).collect();
        let mut gofs = Vec::with_capacity(self.batch);
        for i in 0..self.batch {
            gofs.push(Gof {
                input: rates[i],
                output: rates[i],
                ad: gof[i].fit.ad,
                ada: gof[i].fit.ada,
                chs: gof[i].fit.chs,
                kp: gof[i].fit.kp,
                ks: gof[i].fit.ks,
                ksa: gof[i].fit.ksa,
                n: gof[i].n,
            })
        }
        gofs
    }

    /// Selects from among a number of simulated accumulation records, the record most
    /// characteristic of the average number and age distribution of deposits.
    ///  - `self` is a model with reservoir characteristics set.
    ///  - `bins` is the number of bins with which to construct a cdf.
    ///  - Returns the record with the lowest loss statistic.
    ///
    /// For large numbers of runs, the cdf becomes a large vector.  Selecting a smaller
    /// number of `bins` reduces memory consumption and increases speed, but can reduce accuracy.
    ///
    /// The loss statistic is the K-S stat plus a normalized 'distance from average number
    /// of deposits'.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///
    ///     let mut debris_flows = Reservoir::new()
    ///         .input(&0.862)?
    ///         .output(&0.862)?;
    ///
    ///     // model parameters
    ///     let period = 3000.0; // run simulations for 30000 years
    ///     let runs = 20; // run 1000 simulated accumulations per candidate pair for goodness-of-fit
    ///     let bins = 500; // split observation into bins for deriving CDF
    ///
    ///     // create reservoir model using builder pattern
    ///     let mut model = Model::new(debris_flows)
    ///         .period(&period)
    ///         .runs(runs);
    ///
    ///     // sample a stereotypical record from 1000 runs of 30000 years
    ///     let eg = model.stereotype(bins);
    ///     let egx = model.stereotype(bins);
    ///     // compare the CDF of the synthetic example to the observed debris-flow deposit record
    ///     plot::comp_cdf(&eg, &egx, "examples/transit_stereotype.png");
    ///
    ///     Ok(())
    /// }
    ///```
    pub fn stereotype(&mut self, bins: usize) -> Vec<f64> {
        let mut res: Vec<Model> = Vec::with_capacity(self.runs);
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        let seeds: Vec<u64> = seeder
            .sample_iter(&mut self.reservoir.range)
            .take(self.runs)
            .collect();

        for seed in seeds {
            // make boot number copies of reservoir
            res.push(self.clone().reservoir(self.reservoir.clone().range(seed)));
        }
        let res: Vec<Reservoir> = res.par_iter().cloned().map(|x| x.reservoir.sim()).collect(); // simulate accumulation record for each copy
        let mut ns: Vec<f64> = res
            .par_iter()
            .cloned()
            .map(|x| x.mass.len() as f64)
            .collect(); // number of deposits in reservoir
        let mid_n = utils::median(&ns); // median number of deposits
        ns = ns
            .iter()
            .map(|x| rand_distr::num_traits::abs((x / mid_n) - 1.0))
            .collect(); // distance from median length
                        // collect reservoir masses into single vector and calculate the cdf
        let mut rec = Vec::new(); // vector of mass
        for r in res.clone() {
            rec.append(&mut r.mass.clone()); // add each run to make one long vector
        }
        let cdf = utils::cdf_bin(&rec, bins); // subsample vector to length bins

        // TODO:  parallelize
        let gof: Vec<Fit> = res.par_iter().cloned().map(|x| x.gof1(&cdf)).collect(); // ks and kp values
        let ks: Vec<f64> = gof.par_iter().cloned().map(|x| x.ks).collect(); // clip to just ks values
        let mut least = 1.0; // test for lowest fit (set to high value)
        let mut low = Reservoir::new(); // initialize variable to hold lowest fit
        for (i, val) in ns.iter().enumerate() {
            let loss = ks[i] + val; // loss function
            if loss < least {
                // if lowest value
                low = res[i].clone(); // copy to low
                least = loss; // set least to new low value
            }
        }

        low.mass
    }

    /// Produces a raw vector of observations of simulated transit times from multiple runs.
    /// Used to produce statistics on the mean, median and statistical moments.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///
    ///     let period: f64 = 2000.0;  // length of time to simulate accumulation in the reservoir in years
    ///     let runs: usize = 20; // number of simulations to run for estimation
    ///     let df_rt: f64 = 0.72; // rate for steady state debris-flow accumulation
    ///     let until = 4095; // range limit for tracking transit times
    ///
    ///     // to estimate transit times, omit inherit age from charcoal
    ///     let debris_flows = Reservoir::new().input(&df_rt)?.output(&df_rt)?;
    ///     let mut df_mod = Model::new(debris_flows).period(&period).runs(runs).range(until);
    ///
    ///     // vector of transit times
    ///     let df_t = df_mod.transit_times();
    ///     println!("Debris-flow transit time quantiles are {:?}", utils::quantiles(&df_t));
    ///
    ///     Ok(())
    /// }
    /// ```
    pub fn transit_times(&mut self) -> Vec<f64> {
        let mut res = Vec::with_capacity(self.runs);
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        let seeds: Vec<u64> = seeder
            .sample_iter(&mut self.reservoir.range)
            .take(self.runs)
            .collect();
        for seed in seeds {
            res.push(self.reservoir.clone().range(seed));
        }
        res = res.par_iter().cloned().map(|x| x.sim()).collect();

        let index = 0..self.range;
        let mut out = vec![Complex::new(0.0, 0.0); index.len()];
        let mut real_planner = RealFftPlanner::<f64>::new();
        info!("Constructing FftPlanner for fourier transforms.");
        let r2c = real_planner.plan_fft_forward(index.len() + 1);
        for r in res {
            let cdf = utils::cdf_rng(&r.mass, &index);
            let mut pmf = utils::pmf_from_cdf(&cdf);
            let mut spectrum = r2c.make_output_vec();
            info!("Forward transforming the pmf using fft.");
            r2c.process(&mut pmf, &mut spectrum).unwrap();
            info!("Summing transformed pmfs.");
            out = out
                .iter()
                .zip(spectrum.iter())
                .map(|(a, b)| a + b)
                .collect::<Vec<Complex<f64>>>();
        }
        info!("Constructing FftPlanner for inverse fourier transforms.");
        let c2r = real_planner.plan_fft_inverse(index.len() + 1);
        info!("Inverse fourier transform using fft.");
        let mut out_data = c2r.make_output_vec();
        c2r.process(&mut out, &mut out_data).unwrap();
        info!("Normalize output by dividing by length.");
        out_data = out_data
            .par_iter()
            .map(|a| a / index.len() as f64)
            .collect::<Vec<f64>>();
        info!("Normalize output by dividing by sum of pmfs (having added several pmfs together).");
        let sum_out = out_data.iter().fold(0.0, |acc, x| acc + *x);
        out_data = out_data
            .par_iter()
            .map(|a| a / sum_out)
            .collect::<Vec<f64>>();

        out_data
    }
}

impl Default for Model {
    fn default() -> Self {
        Model {
            reservoir: Reservoir::new(),
            period: 30000.0,
            runs: 100,
            batch: 10,
            duration: 1,
            range: 10000,
        }
    }
}

/// Model manager struct for control flow of modeling operations.
#[derive(Clone, Debug)]
pub struct ModelManager {
    batch: usize,
    capture_gravels: std::ops::Range<f64>,
    capture_fines: std::ops::Range<f64>,
    duration: u64,
    fines: bool,
    index: std::ops::Range<i32>,
    obs: Vec<f64>,
    obs_len: usize,
    period: f64,
    periods: std::ops::Range<f64>,
    range: rand::rngs::StdRng,
    rates: std::ops::Range<f64>,
    runs: usize,
    source_runs: usize,
    storage_gravels: std::ops::Range<f64>,
    storage_fines: std::ops::Range<f64>,
    thresholds: Thresholds,
    turnover: std::ops::Range<f64>,
}

/// Methods for modeling.
impl ModelManager {
    /// Reference a FluvialModel type to create a new ModelManager instance.
    pub fn new() -> ModelManager {
        ModelManager {
            batch: 1,
            capture_fines: 0.0..1.0,
            capture_gravels: 0.0..1.0,
            duration: 1,
            fines: false,
            index: 0..1000,
            obs: Vec::new(),
            obs_len: 0,
            period: 100.0,
            periods: 100.0..1000.0,
            range: rand::SeedableRng::seed_from_u64(777),
            rates: 0.01..2.0,
            runs: 10,
            source_runs: 10,
            storage_fines: 0.0..1.0,
            storage_gravels: 0.0..1.0,
            thresholds: Thresholds::new(0.0, 0.0, 0.0, 0.0),
            turnover: 200.0..300.0,
        }
    }

    /// Set the batch size for runs.
    pub fn batch(mut self, batch: usize) -> Self {
        self.batch = batch;
        self
    }

    /// Set capture rate of fines.
    pub fn capture_fines(mut self, capture_fines: std::ops::Range<f64>) -> Self {
        self.capture_fines = capture_fines;
        self
    }

    /// Set capture rate of gravels.
    pub fn capture_gravels(mut self, capture_gravels: std::ops::Range<f64>) -> Self {
        self.capture_gravels = capture_gravels;
        self
    }

    /// Set duration of timed runs.
    pub fn duration(mut self, duration: u64) -> Self {
        self.duration = duration;
        self
    }

    /// Set to true to produce fines with Fluvial::sim().
    pub fn fines(mut self, fines: bool) -> Self {
        self.fines = fines;
        self
    }

    /// Set the index range for transit_times().
    pub fn index(mut self, index: std::ops::Range<i32>) -> Self {
        self.index = index;
        self
    }

    /// Set period length of model runs.
    pub fn obs(mut self, obs: &[f64]) -> Self {
        self.obs = obs.to_owned();
        self
    }

    /// Set number of observations to collect for representative sample.
    pub fn obs_len(mut self, obs: &[f64]) -> Self {
        self.obs_len = obs.len();
        self
    }

    /// Set period length of model runs.
    pub fn period(mut self, period: f64) -> Self {
        self.period = period;
        self
    }

    /// Set the period range for Reservoir::fit_rates().
    pub fn periods(mut self, periods: std::ops::Range<f64>) -> Self {
        self.periods = periods;
        self
    }

    /// Set model seed.
    pub fn range(mut self, seed: u64) -> Self {
        self.range = rand::SeedableRng::seed_from_u64(seed);
        self
    }

    /// Set the range of input/output rates for Reservoir::fit_rates().
    pub fn rates(mut self, rates: std::ops::Range<f64>) -> Self {
        self.rates = rates;
        self
    }

    /// Set number of model runs.
    pub fn runs(mut self, runs: usize) -> Self {
        self.runs = runs;
        self
    }

    /// Return a vector of struct clones with unique but reproducible seeds.
    pub fn seed_clones(mut self) -> Vec<ModelManager> {
        let mut res = Vec::with_capacity(self.runs);
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        for _ in 0..self.runs {
            res.push(self.clone().range(seeder.sample(&mut self.range)));
        }
        res
    }

    /// Set the number of runs when estimating source mass (per run of Fluvial::sim()).
    pub fn source_runs(mut self, source_runs: usize) -> Self {
        self.source_runs = source_runs;
        self
    }

    /// Set the range of storage rates for fines.
    pub fn storage_fines(mut self, storage_fines: std::ops::Range<f64>) -> Self {
        self.storage_fines = storage_fines;
        self
    }

    /// Set the range of storage rates for gravels.
    pub fn storage_gravels(mut self, storage_gravels: std::ops::Range<f64>) -> Self {
        self.storage_gravels = storage_gravels;
        self
    }

    /// Set statistical thresholds for goodness-of-fit tests.
    pub fn thresholds(mut self, ad: f64, ch: f64, kp: f64, ks: f64) -> Self {
        self.thresholds = Thresholds::new(ad, ch, kp, ks);
        self
    }

    /// Set the range of turnover periods for sediment storage.
    pub fn turnover(mut self, turnover: std::ops::Range<f64>) -> Self {
        self.turnover = turnover;
        self
    }
}

impl Default for ModelManager {
    fn default() -> ModelManager {
        ModelManager::new()
    }
}

/// Struct for model run statistics.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct FluvialFit {
    capture_rate_fines: f64,
    capture_rate_gravels: f64,
    storage_rate_fines: f64,
    storage_rate_gravels: f64,
    turnover: f64,
    ad1: f64,
    ad2: f64,
    ch: f64,
    kp: f64,
    ks1: f64,
    ks2: f64,
}

impl FluvialFit {
    /// Convert csv record to Gof struct.
    pub fn read(path: &str) -> Result<Vec<FluvialFit>, errors::ResError> {
        let mut fit = Vec::new();
        let var = std::fs::File::open(path)?;
        let mut rdr = csv::Reader::from_reader(var);
        for result in rdr.records() {
            let row = result?;
            let row: FluvialFit = row.deserialize(None)?;
            fit.push(row);
        }
        Ok(fit)
    }

    /// Write statistical results to csv file.
    pub fn record(rec: &mut Vec<FluvialFit>, title: &str) -> Result<(), errors::ResError> {
        let mut wtr = csv::Writer::from_path(title)?;
        for i in rec {
            wtr.serialize(i)?;
        }
        wtr.flush()?;
        Ok(())
    }
}

/// Fluvial traversal struct for gravels and fines.
#[derive(Clone, Debug)]
pub struct Fluvial {
    capture_rate_fines: f64,
    capture_rate_gravels: f64,
    manager: ModelManager,
    mass: Vec<f64>,
    source: Vec<f64>,
    storage_rate_fines: f64,
    storage_rate_gravels: f64,
    turnover: f64,
}

impl Fluvial {
    /// New fluvial struct
    pub fn new() -> Fluvial {
        Fluvial {
            capture_rate_fines: 0.0,
            capture_rate_gravels: 0.0,
            manager: ModelManager::new(),
            mass: Vec::new(),
            source: Vec::new(),
            storage_rate_fines: 0.0,
            storage_rate_gravels: 0.0,
            turnover: 0.0,
        }
    }

    /// Cherry pick the closest fit to an observed distribution.
    pub fn cherry_pick(self) -> Vec<f64> {
        let sims = self
            .clone()
            .manager
            .seed_clones()
            .par_iter()
            .map(|x| self.clone().manager(x).sim())
            .collect::<Vec<Fluvial>>();
        let fit = sims
            .par_iter()
            .map(|s| s.clone().gof(&self.manager.obs))
            .collect::<Vec<Vec<f64>>>();
        let min_ks = fit
            .iter()
            .map(|f| f[4])
            .fold(f64::INFINITY, |a, b| a.min(b));
        let min_id = fit
            .par_iter()
            .enumerate()
            .filter(|(_, val)| (val[4] - min_ks) < 0.0001)
            .map(|(i, _)| i)
            .collect::<Vec<usize>>();
        sims[min_id[0]].clone().mass
    }

    /// Hit rate for statistical thresholds.
    pub fn hit_rate(self) -> Vec<f64> {
        let sims = self
            .clone()
            .manager
            .seed_clones()
            .par_iter()
            .map(|x| self.clone().manager(x).sim())
            .collect::<Vec<Fluvial>>();
        let fit = sims
            .par_iter()
            .map(|s| s.clone().gof(&self.manager.obs))
            .collect::<Vec<Vec<f64>>>();
        let hit_ad = fit
            .par_iter()
            .filter(|f| f[0] <= self.manager.thresholds.ad)
            .count() as f64
            / self.manager.runs as f64;
        let hit_ch = fit
            .par_iter()
            .filter(|f| f[2] <= self.manager.thresholds.ch)
            .count() as f64
            / self.manager.runs as f64;
        let hit_kp = fit
            .par_iter()
            .filter(|f| f[3] <= self.manager.thresholds.kp)
            .count() as f64
            / self.manager.runs as f64;
        let hit_ks = fit
            .par_iter()
            .filter(|f| f[4] <= self.manager.thresholds.ks)
            .count() as f64
            / self.manager.runs as f64;
        vec![hit_ad, hit_ch, hit_kp, hit_ks]
    }

    /// Fits a given flux probability and storage rate to an empiric record.
    pub fn hit_rates(mut self) -> FluvialFit {
        let capture_fines = rand::distributions::Uniform::from(self.manager.capture_fines.clone());
        let capture_rate_fines = capture_fines.sample(&mut self.manager.range);
        let capture_gravels =
            rand::distributions::Uniform::from(self.manager.capture_gravels.clone());
        let capture_rate_gravels = capture_gravels.sample(&mut self.manager.range);
        let storage_fines = rand::distributions::Uniform::from(self.manager.storage_fines.clone());
        let storage_rate_fines = storage_fines.sample(&mut self.manager.range);
        let storage_gravels =
            rand::distributions::Uniform::from(self.manager.storage_gravels.clone());
        let storage_rate_gravels = storage_gravels.sample(&mut self.manager.range);
        let turnover_range = rand::distributions::Uniform::from(self.manager.turnover.clone());
        let turnover = turnover_range.sample(&mut self.manager.range);

        let fit = self
            .capture_rate_fines(capture_rate_fines)
            .capture_rate_gravels(capture_rate_gravels)
            .storage_rate_fines(storage_rate_fines)
            .storage_rate_gravels(storage_rate_gravels)
            .turnover(&turnover)
            .hit_rate();
        FluvialFit {
            capture_rate_fines,
            capture_rate_gravels,
            storage_rate_fines,
            storage_rate_gravels,
            turnover,
            ad1: fit[0],
            ad2: 0.0,
            ch: fit[1],
            kp: fit[2],
            ks1: fit[3],
            ks2: 0.0,
        }
    }

    /// Run fit_rates() for a set duration.
    pub fn hit_rates_timed(mut self, path: &str) -> Result<Vec<FluvialFit>, errors::ResError> {
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);

        let dur = std::time::Duration::new(60 * 60 * self.manager.duration, 0);
        let now = std::time::SystemTime::now();
        let mut rec = Vec::new();
        let exists = std::path::Path::new(path).exists();
        match exists {
            true => {
                let mut stats: Vec<FluvialFit> = FluvialFit::read(path)?;
                rec.append(&mut stats);
            }
            false => {}
        }
        while std::time::SystemTime::now() < now + dur {
            let mut fit = self.clone();
            fit.manager = fit
                .manager
                .range(seeder.sample(&mut self.manager.range))
                .clone();
            let new = fit.hit_rates();
            {
                rec.push(new);
            }
            FluvialFit::record(&mut rec, path)?;
        }
        Ok(rec)
    }

    /// Fit number of runs to gof tests and return mean of each.
    pub fn fit(self, other: &[f64]) -> Vec<f64> {
        let mut rng = self.manager.range.clone();
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        let seeds: Vec<u64> = seeder
            .sample_iter(&mut rng)
            .take(self.manager.runs)
            .collect();

        let mut ads = Vec::with_capacity(self.manager.runs as usize);
        let mut adas = Vec::with_capacity(self.manager.runs as usize);
        let mut chs = Vec::with_capacity(self.manager.runs as usize);
        let mut kps = Vec::with_capacity(self.manager.runs as usize);
        let mut kss = Vec::with_capacity(self.manager.runs as usize);
        let mut ksas = Vec::with_capacity(self.manager.runs as usize);

        for seed in seeds {
            let fit = self
                .clone()
                .manager(&self.manager.clone().range(seed))
                .gof(other);
            ads.push(fit[0]);
            adas.push(fit[1]);
            chs.push(fit[2]);
            kps.push(fit[3]);
            kss.push(fit[4]);
            ksas.push(fit[5]);
        }

        let res = vec![
            utils::mean(&ads),
            utils::mean(&adas),
            utils::mean(&chs),
            utils::mean(&kps),
            utils::mean(&kss),
            utils::mean(&ksas),
        ];
        res
    }

    /// Fits a given flux probability and storage rate to an empiric record.
    pub fn fit_rate(mut self, other: &[f64]) -> FluvialFit {
        let capture_fines = rand::distributions::Uniform::from(self.manager.capture_fines.clone());
        let capture_rate_fines = capture_fines.sample(&mut self.manager.range);
        let capture_gravels =
            rand::distributions::Uniform::from(self.manager.capture_gravels.clone());
        let capture_rate_gravels = capture_gravels.sample(&mut self.manager.range);
        let storage_fines = rand::distributions::Uniform::from(self.manager.storage_fines.clone());
        let storage_rate_fines = storage_fines.sample(&mut self.manager.range);
        let storage_gravels =
            rand::distributions::Uniform::from(self.manager.storage_gravels.clone());
        let storage_rate_gravels = storage_gravels.sample(&mut self.manager.range);
        let turnover_range = rand::distributions::Uniform::from(self.manager.turnover.clone());
        let turnover = turnover_range.sample(&mut self.manager.range);

        let fit = self
            .capture_rate_fines(capture_rate_fines)
            .capture_rate_gravels(capture_rate_gravels)
            .storage_rate_fines(storage_rate_fines)
            .storage_rate_gravels(storage_rate_gravels)
            .turnover(&turnover)
            .fit(other);
        FluvialFit {
            capture_rate_fines,
            capture_rate_gravels,
            storage_rate_fines,
            storage_rate_gravels,
            turnover,
            ad1: fit[1],
            ad2: fit[0],
            ch: fit[2],
            kp: fit[3],
            ks1: fit[5],
            ks2: fit[4],
        }
    }

    /// Return a stereotypical mass age distribution at the current model parameters.
    pub fn stereotype_rate(self) -> Vec<f64> {
        // let bins = 1000;
        let mut rng = self.manager.range.clone();
        let mut res: Vec<Fluvial> = Vec::with_capacity(self.manager.runs);
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        let seeds: Vec<u64> = seeder
            .sample_iter(&mut rng)
            .take(self.manager.runs)
            .collect();

        for seed in seeds {
            // make boot number copies of reservoir
            res.push(self.clone().manager(&self.manager.clone().range(seed)));
        }
        res = res
            .par_iter()
            .cloned()
            .map(|x| x.sim())
            .collect::<Vec<Fluvial>>(); // simulate accumulation record for each copy
        let mut ns = res
            .par_iter()
            .cloned()
            .map(|x| x.mass.len() as f64)
            .collect::<Vec<f64>>(); // number of deposits in reservoir
        let mid_n = utils::median(&ns); // median number of deposits
        ns = ns
            .par_iter()
            .map(|x| rand_distr::num_traits::abs((x / mid_n) - 1.0))
            .collect::<Vec<f64>>(); // distance from median length
                                    // collect reservoir masses into single vector and calculate the cdf
        let mut rec = Vec::new(); // vector of mass
        for r in res.clone() {
            rec.append(&mut r.mass.clone()); // add each run to make one long vector
        }
        // let cdf = utils::cdf_bin(&rec, bins); // subsample vector to length bins

        let fit = res
            .par_iter()
            .cloned()
            .map(|x| x.gof(&rec))
            .collect::<Vec<Vec<f64>>>(); // ks and kp values
        let ks = fit.par_iter().cloned().map(|x| x[5]).collect::<Vec<f64>>(); // clip to just ks values
        let mut least = 1.0; // test for lowest fit (set to high value)
        let mut low = Fluvial::new(); // initialize variable to hold lowest fit
        for (i, val) in ns.iter().enumerate() {
            let loss = ks[i] + val; // loss function
            if loss < least {
                // if lowest value
                low = res[i].clone(); // copy to low
                least = loss; // set least to new low value
            }
        }

        low.mass
    }

    /// Fit a range of flux probabilities and storage rates to an empiric record.
    pub fn fit_rates(mut self, other: &[f64]) -> Vec<FluvialFit> {
        // let mut rng = self.manager.range.clone();
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        let mut res = Vec::new();
        for _ in 0..self.manager.batch {
            let mut fit = self.clone();
            fit.manager = fit
                .manager
                .range(seeder.sample(&mut self.manager.range))
                .clone();
            res.push(fit.fit_rate(other));
        }
        res
    }

    /// Run fit_rates() for a set duration.
    pub fn fit_rates_timed(
        mut self,
        other: &[f64],
        path: &str,
    ) -> Result<Vec<FluvialFit>, errors::ResError> {
        // let mut rng = self.manager.range.clone();
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);

        let dur = std::time::Duration::new(60 * 60 * self.manager.duration, 0);
        let now = std::time::SystemTime::now();
        let mut rec = Vec::new();
        let exists = std::path::Path::new(path).exists();
        match exists {
            true => {
                let mut stats: Vec<FluvialFit> = FluvialFit::read(path)?;
                rec.append(&mut stats);
            }
            false => {}
        }
        while std::time::SystemTime::now() < now + dur {
            let mut fit = self.clone();
            fit.manager = fit
                .manager
                .range(seeder.sample(&mut self.manager.range))
                .clone();
            let mut new = fit.fit_rates(other);
            {
                rec.append(&mut new);
            }
            FluvialFit::record(&mut rec, path)?;
        }
        Ok(rec)
    }

    /// Represents the probability of fines routing to the storage pool.
    pub fn capture_rate_fines(mut self, capture_rate_fines: f64) -> Self {
        self.capture_rate_fines = capture_rate_fines;
        self
    }

    /// Represents the probability of gravels routing to the storage pool.
    pub fn capture_rate_gravels(mut self, capture_rate_gravels: f64) -> Self {
        self.capture_rate_gravels = capture_rate_gravels;
        self
    }

    /// Goodness-of-fit test suite.
    pub fn gof(self, other: &[f64]) -> Vec<f64> {
        utils::gof(&self.sim().mass, other)
    }

    /// Assign ModelManager to Fluvial struct
    pub fn manager(mut self, manager: &ModelManager) -> Self {
        self.manager = manager.to_owned();
        self
    }

    /// Runs sim() a number of times specified by ModelManager struct.
    pub fn n_sim(self) -> Vec<f64> {
        /*let mut rec = Vec::new();
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);

        for _ in 0..self.manager.source_runs {
            let mut fit = self.clone();
            fit.manager = fit.manager.range(seeder.sample(&mut self.manager.range)).clone();
            let mut new = fit.sim().mass;
            {
                rec.append(&mut new);
            }
        }
        */
        let mut res = Vec::new();
        let _ = self
            .clone()
            .manager
            .seed_clones()
            .iter()
            .map(|x| res.extend(self.clone().manager(x).sim().mass))
            .collect::<()>();
        res
    }

    /// Prints csv of reservoir mass to file path.
    pub fn record_mass(mut self, path: &str) -> Result<(), errors::ResError> {
        utils::record(&mut self.mass, path)?;
        Ok(())
    }

    /// Simulates removal from the reservoir over time based on the rate.
    pub fn sim(mut self) -> Self {
        let wts = rand::distributions::WeightedIndex::new(&self.source).unwrap();
        let mut index = Vec::new();
        for i in self.manager.index.clone() {
            index.push(i as f64);
        }
        let mut source_flux = Vec::new();
        for _ in 0..self.manager.obs_len {
            source_flux.push(index[wts.sample(&mut self.manager.range)]);
        }
        info!("Selection probability for storage.");
        let mut idx = Vec::new();
        let mut ps = Vec::new();
        let mut thresh = 0.0;
        for i in 0..self.manager.period as i32 {
            idx.push(i as f64);
            match self.manager.fines {
                true => {
                    thresh = self.capture_rate_fines;
                    ps.push(utils::fish(
                        self.storage_rate_fines,
                        i as f64 / self.turnover,
                    ));
                }
                false => {
                    thresh = self.capture_rate_gravels;
                    ps.push(utils::fish(
                        self.storage_rate_gravels,
                        i as f64 / self.turnover,
                    ));
                }
            }
        }
        let wts = rand::distributions::WeightedIndex::new(&ps).unwrap();
        for item in &mut source_flux {
            let roll = self.manager.range.gen_range(0.0..1.0);
            if roll < thresh {
                *item += idx[wts.sample(&mut self.manager.range)];
            }
        }

        self.mass = source_flux;
        self
    }

    /// Sets source flux for reservoirs.
    pub fn source(mut self, source: Vec<f64>) -> Self {
        let source_flux = utils::cdf_rng(&source, &self.manager.index);
        // let source_flux = source.clone().transit_times();

        // if self.manager.fines {
        //     let source_gravel = source.model(&model.fines(false)).transit_times();
        //     source_flux.extend(source_gravel);
        // }
        self.source = source_flux;
        self
    }

    /// Sets source flux from csv.
    /// To save and reuse large samples that are computationally intensive.
    pub fn source_from_csv(mut self, path: &str) -> Result<Self, errors::ResError> {
        let source = utils::read_f64(path)?;
        self.source = source;
        Ok(self)
    }

    /// Sets the rate of the Poisson distribution defining the PMF of storage time for fines.
    pub fn storage_rate_fines(mut self, storage_rate_fines: f64) -> Self {
        self.storage_rate_fines = storage_rate_fines;
        self
    }

    /// Sets the rate of the Poisson distribution defining the PMF of storage time for fines.
    pub fn storage_rate_gravels(mut self, storage_rate_gravels: f64) -> Self {
        self.storage_rate_gravels = storage_rate_gravels;
        self
    }

    /// Return CDF of transit times based on model parameters.
    pub fn transit_times(self) -> Vec<f64> {
        let res = self
            .clone()
            .manager
            .seed_clones()
            .par_iter()
            .map(|x| self.clone().manager(x).sim())
            .collect::<Vec<Fluvial>>();

        let index = self.manager.index;
        let mut out = vec![Complex::new(0.0, 0.0); index.len()];
        let mut real_planner = RealFftPlanner::<f64>::new();
        info!("Constructing FftPlanner for fourier transforms.");
        let r2c = real_planner.plan_fft_forward(index.len() + 1);
        for r in res {
            let cdf = utils::cdf_rng(&r.mass, &index);
            let mut pmf = utils::pmf_from_cdf(&cdf);
            let mut spectrum = r2c.make_output_vec();
            info!("Forward transforming the pmf using fft.");
            r2c.process(&mut pmf, &mut spectrum).unwrap();
            info!("Summing transformed pmfs.");
            out = out
                .iter()
                .zip(spectrum.iter())
                .map(|(a, b)| a + b)
                .collect::<Vec<Complex<f64>>>();
        }
        info!("Constructing FftPlanner for inverse fourier transforms.");
        let c2r = real_planner.plan_fft_inverse(index.len() + 1);
        info!("Inverse fourier transform using fft.");
        let mut out_data = c2r.make_output_vec();
        c2r.process(&mut out, &mut out_data).unwrap();
        info!("Normalize output by dividing by length.");
        out_data = out_data
            .par_iter()
            .map(|a| a / index.len() as f64)
            .collect::<Vec<f64>>();
        info!("Normalize output by dividing by sum of pmfs (having added several pmfs together).");
        let sum_out = out_data.iter().fold(0.0, |acc, x| acc + *x);
        out_data = out_data
            .par_iter()
            .map(|a| a / sum_out)
            .collect::<Vec<f64>>();

        out_data
    }

    /// Sets length of the turnover period.
    pub fn turnover(mut self, turnover: &f64) -> Self {
        self.turnover = turnover.to_owned();
        self
    }
}

impl Default for Fluvial {
    fn default() -> Fluvial {
        Fluvial::new()
    }
}

/// Struct for recording reservoir characteristics.
#[derive(Clone, Debug)]
pub struct Reservoir {
    input: Option<Exp<f64>>,
    model: ModelManager,
    /// Ages (in years) of deposits accumulated in reservoir.
    pub mass: Vec<f64>,
    output: Option<Exp<f64>>,
    flux: Vec<f64>,
    inherit: Option<Vec<f64>>,
    range: rand::rngs::StdRng,
}

impl Reservoir {
    /// Fit reservoir to observed record.
    pub fn fit_reservoir(self) -> Vec<f64> {
        let reservoirs = self
            .clone()
            .model
            .seed_clones()
            .par_iter()
            .map(|x| self.clone().model(x).sim())
            .collect::<Vec<Reservoir>>();
        let fits = reservoirs
            .par_iter()
            .map(|x| utils::gof(&x.mass, &self.model.obs))
            .collect::<Vec<Vec<f64>>>();
        let ad = fits.par_iter().map(|x| x[0]).collect::<Vec<f64>>();
        let ch = fits.par_iter().map(|x| x[2]).collect::<Vec<f64>>();
        let kp = fits.par_iter().map(|x| x[3]).collect::<Vec<f64>>();
        let ks = fits.par_iter().map(|x| x[4]).collect::<Vec<f64>>();
        let ns = reservoirs
            .par_iter()
            .map(|x| x.mass.len() as f64)
            .collect::<Vec<f64>>();
        vec![
            utils::mean(&ad),
            utils::mean(&ch),
            utils::mean(&kp),
            utils::mean(&ks),
            utils::mean(&ns),
        ]
    }

    /// Fit reservoirs to observed record.
    pub fn fit_reservoirs(mut self) -> Result<Vec<ReservoirFit>, errors::ResError> {
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        let seeds = seeder
            .sample_iter(&mut self.model.range)
            .take(self.model.batch)
            .collect::<Vec<u64>>();
        let rates = rand::distributions::Uniform::from(self.model.rates.clone())
            .sample_iter(&mut self.model.range)
            .take(self.model.batch)
            .collect::<Vec<f64>>();
        let periods = rand::distributions::Uniform::from(self.model.periods.clone())
            .sample_iter(&mut self.model.range)
            .take(self.model.batch)
            .collect::<Vec<f64>>();

        let mut reservoirs = Vec::with_capacity(self.model.batch);
        for i in 0..self.model.batch {
            let model = self.model.clone().period(periods[i]).range(seeds[i]);
            reservoirs.push(
                self.clone()
                    .input(&rates[i])?
                    .output(&rates[i])?
                    .model(&model),
            );
        }

        let fits = reservoirs
            .iter()
            .map(|x| x.clone().fit_reservoir())
            .collect::<Vec<Vec<f64>>>();
        let ad = fits.iter().map(|x| x[0]).collect::<Vec<f64>>();
        let ch = fits.iter().map(|x| x[1]).collect::<Vec<f64>>();
        let kp = fits.iter().map(|x| x[2]).collect::<Vec<f64>>();
        let ks = fits.iter().map(|x| x[3]).collect::<Vec<f64>>();
        let ns = fits.iter().map(|x| x[4]).collect::<Vec<f64>>();

        let mut gofs = Vec::with_capacity(self.model.batch);
        for i in 0..self.model.batch {
            gofs.push(ReservoirFit {
                rate: rates[i],
                period: periods[i],
                ad: ad[i],
                ch: ch[i],
                kp: kp[i],
                ks: ks[i],
                n: ns[i],
            });
        }

        Ok(gofs)
    }

    /// Timed fit of reservoirs to observed record.
    pub fn fit_reservoirs_timed(
        mut self,
        path: &str,
    ) -> Result<Vec<ReservoirFit>, errors::ResError> {
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);

        let dur = std::time::Duration::new(60 * 60 * self.model.duration, 0);
        let now = std::time::SystemTime::now();
        let mut rec = Vec::new();
        let exists = std::path::Path::new(path).exists();
        match exists {
            true => {
                let mut stats: Vec<ReservoirFit> = ReservoirFit::read(path)?;
                rec.append(&mut stats);
            }
            false => {}
        }
        while std::time::SystemTime::now() < now + dur {
            let model = self
                .model
                .clone()
                .range(seeder.sample(&mut self.model.range));
            let fit = self.clone().model(&model);
            let mut new = fit.fit_reservoirs()?;
            {
                rec.append(&mut new);
            }
            ReservoirFit::record(&mut rec, path)?;
        }
        Ok(rec)
    }

    /// Goodness-of-fit test suite.
    pub fn gof(self, other: &[f64]) -> Vec<f64> {
        utils::gof(&self.mass, other)
    }

    /// Compare the accumulated mass in a reservoir to another record.
    /// Produces two goodness-of-fit statistics in a tuple:
    /// the K-S statistic and the Kuiper statistic, respectively.
    /// Called by [fit_rate](#method.fit_rate), you can use it on individual records too.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     let ages = vec![10.0, 42.0, 77.7, 12.0, 99.9, 10000.0, 777.7];
    ///     let mut debris_flows = Reservoir::new()
    ///         .input(&0.78)?
    ///         .output(&0.78)?
    ///         .sim();
    ///     let fit = debris_flows.gof(&ages);
    ///     println!("Fit is {:?}.", fit);
    ///     Ok(())
    /// }
    /// ```
    pub fn gof1(&self, other: &[f64]) -> Fit {
        let cdf = utils::cdf_dual(&self.mass, other);
        let ad = utils::ad_dual(&self.mass, other);
        // anderson-darling test
        let lnx = self.mass.len();
        let lny = other.len();
        let k = lnx + lny;
        let ada = (2.492 - 1.0) * (1.0 - (1.55 / k as f64)) + 1.0;
        // chi-squared pearsons test
        let chs = cdf
            .iter()
            .filter(|x| x.0 > 0.0)
            .map(|x| f64::powi(x.1 - x.0, 2) / x.0)
            .sum::<f64>();
        // kuiper test
        let kp1 = cdf.iter().map(|x| x.0 - x.1).fold(0.0, f64::max);
        let kp2 = cdf.iter().map(|x| x.1 - x.0).fold(0.0, f64::max);
        let kp = kp1 + kp2;
        // kolmogorov-smirnov test
        let ks = cdf
            .iter()
            .map(|x| rand_distr::num_traits::abs(x.0 - x.1))
            .fold(0.0, f64::max);
        let ksa = 1.35810 * f64::sqrt(k as f64 / (lnx * lny) as f64); // k-s crit value at 0.05
        Fit {
            ad,
            ada,
            chs,
            kp,
            ks,
            ksa,
        }
    }

    /// Inherited age refers to age of charcoal upon entering the reservoir.
    /// Multiple samples of charcoal from a single deposit produces a vector of inherited ages,
    /// represented by the mean expected age of each charcoal sample in a f64 vector.
    /// The sample age of charcoal is the sum of its inherited age plus transit time through the reservoir.
    /// When simulating a reservoir model, each event entering the reservoir receives
    /// a random amount of inherited age sampled from the vector `ages`.
    ///
    /// # Examples
    ///
    /// ```
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     let start_ages = vec![7.0, 42.0, 401.0, 1234.5, 7777.7, 5.2, 0.1];
    ///     let res = Reservoir::new().inherit(&start_ages);
    ///     Ok(())
    /// }
    /// ```
    pub fn inherit(mut self, ages: &[f64]) -> Self {
        self.inherit = Some(ages.to_vec());
        self
    }

    /// Assign an input rate to a reservoir.
    /// Converts a reference to a float 64 `rate` into an exponential distribution with lamdba `rate` using the rand crate.
    ///
    /// # Examples
    ///
    /// ```
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     // new reservoirs have no input rate, call input() to set one
    ///     let mut res = Reservoir::new().input(&0.58)?;
    ///     Ok(())
    /// }
    /// ```
    pub fn input(mut self, rate: &f64) -> Result<Self, errors::ResError> {
        let rate = Exp::new(*rate)?;
        self.input = Some(rate);
        Ok(self)
    }

    /// Set model parameters as fields in manager struct.
    pub fn model(mut self, model: &ModelManager) -> Self {
        self.model = model.to_owned();
        self
    }

    /// Create reservoirs using a builder pattern.  Calling new() creates an empty reservoir.
    /// Use the [input](#method.input) and [output](#method.output) methods to set rates, which start at None.
    /// Set inherited age similarly using the method [inherit](#method.inherit).
    ///
    /// # Examples
    /// ```
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     let mut res = Reservoir::new();
    ///     Ok(())
    /// }
    /// ```
    pub fn new() -> Self {
        Reservoir {
            input: None,
            model: ModelManager::new(),
            mass: Vec::new(),
            output: None,
            flux: Vec::new(),
            inherit: None,
            range: rand::rngs::StdRng::from_entropy(),
        }
    }

    /// Runs sim() a number of times specified by ModelManager struct.
    pub fn n_sim(mut self) -> Vec<f64> {
        let mut rec = Vec::new();
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);

        for _ in 0..self.model.source_runs {
            let mut fit = self.clone();
            fit.model = fit
                .model
                .range(seeder.sample(&mut self.model.range))
                .clone();
            let mut new = fit.sim().mass;
            {
                rec.append(&mut new);
            }
        }
        rec
    }

    /// Assign an output rate to a reservoir.
    /// Converts a reference to a float 64 `rate` into an exponential distribution with lamdba `rate` using the rand crate.
    ///
    /// # Examples
    ///
    /// ```
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     // new reservoirs have no output rate, call input() to set one
    ///     let mut res = Reservoir::new().output(&0.58)?;
    ///     Ok(())
    /// }
    /// ```
    pub fn output(mut self, rate: &f64) -> Result<Self, errors::ResError> {
        let rate = Exp::new(*rate)?;
        self.output = Some(rate);
        Ok(self)
    }

    /// Assign a seed to the random number generator, for reproducibility.
    ///  - `seed` is a number to convert in an RNG seed using the rand crate.
    ///
    /// # Examples
    ///
    /// ```
    /// use reservoirs::prelude::*;
    /// fn main() -> Result<(), ResError> {
    ///     // new reservoirs have a random seed, call range() to set a specific seed
    ///     let mut res = Reservoir::new().range(10101);
    ///     Ok(())
    /// }
    /// ```
    pub fn range(mut self, seed: u64) -> Self {
        self.range = rand::SeedableRng::seed_from_u64(seed);
        self
    }

    /// Workhorse function for simulating accumulation records in a reservoir.
    /// Runs simulations on reservoir objects created using the builder pattern.
    /// `period` specifies the amount of time to simulate accumulation in years.
    /// While generally this is a function called in series by other functions, you can use
    /// it to simulate a single accumulation record for a reservoir.
    ///
    /// # Examples
    ///
    /// ```
    /// use reservoirs::prelude::*;
    /// fn main () -> Result<(), ResError> {
    ///     // create reservoirs
    ///     let mut fines = Reservoir::new().input(&0.75)?.output(&0.75)?;
    ///     let mut gravels = Reservoir::new().input(&0.54)?.output(&0.54)?;
    ///
    ///     // simulate accumulation for 30000 years
    ///     fines = fines.sim();
    ///     gravels = gravels.sim();
    ///     Ok(())
    /// }
    /// ```
    pub fn sim(mut self) -> Self {
        // let _ = pretty_env_logger::try_init();
        let mut om = 0f64;
        let mut im = 0f64;
        let mut flux = Vec::new();
        let mut mass = Vec::new(); // time of arrivals in reservoir

        while om < self.model.period {
            info!("Generating a time for removal.");
            match self.output {
                Some(x) => om += x.sample(&mut self.model.range) as f64,
                // TODO: Implement zero rates for input and output
                None => continue,
            }

            while im < om {
                info!("Generating inputs until time for removal.");
                if let Some(x) = self.input {
                    im += x.sample(&mut self.model.range) as f64;
                    mass.push(im);
                }
            }

            if !mass.is_empty() {
                info!("Inputs are available for removal.");
                let mvec: Vec<f64> = mass.par_iter().cloned().filter(|x| x <= &om).collect();
                info!("Selecting from inputs present before time of removal.");
                if !mvec.is_empty() {
                    let rm = rand::distributions::Uniform::from(0..mvec.len())
                        .sample(&mut self.model.range);
                    flux.push(mass[rm]);
                    mass.remove(rm);
                }
            }
        }

        mass = mass.iter().map(|x| self.model.period - x).collect();
        // flux = flux.par_iter().map(|x| period - x).collect();
        if let Some(x) = self.inherit.clone() {
            let ln = x.len();
            mass = mass
                .iter()
                .map(|z| {
                    z + x[rand::distributions::Uniform::from(0..ln).sample(&mut self.model.range)]
                })
                .collect();
            // flux = flux.iter().map(|z| z + x[rand::distributions::Uniform::from(0..ln).sample(&mut self.range)]).collect();
        }

        self.mass = mass;
        self.flux = flux;
        self
    }

    /// Return a stereotypical mass age distribution at the current model parameters.
    pub fn stereotype(&mut self) -> Vec<f64> {
        let mut res: Vec<Reservoir> = Vec::with_capacity(self.model.runs);
        let seeder: rand::distributions::Uniform<u64> =
            rand::distributions::Uniform::new(0, 10000000);
        let seeds = seeder
            .sample_iter(&mut self.model.range)
            .take(self.model.runs)
            .collect::<Vec<u64>>();

        for seed in seeds {
            // make boot number copies of reservoir
            res.push(self.clone().model(&self.model.clone().range(seed)));
        }
        res = res
            .iter()
            .cloned()
            .map(|x| x.sim())
            .collect::<Vec<Reservoir>>(); // simulate accumulation record for each copy
        let mut ns = res
            .iter()
            .cloned()
            .map(|x| x.mass.len() as f64)
            .collect::<Vec<f64>>(); // number of deposits in reservoir
        let mid_n = utils::median(&ns); // median number of deposits
        ns = ns
            .iter()
            .map(|x| rand_distr::num_traits::abs((x / mid_n) - 1.0))
            .collect(); // distance from median length
                        // collect reservoir masses into single vector and calculate the cdf
        let mut rec = Vec::new(); // vector of mass
        for r in res.clone() {
            rec.append(&mut r.mass.clone()); // add each run to make one long vector
        }
        // let cdf = utils::cdf_bin(&rec, bins); // subsample vector to length bins

        let gof = res
            .iter()
            .cloned()
            .map(|x| x.gof1(&rec))
            .collect::<Vec<Fit>>(); // ks and kp values
        let ks = gof.iter().cloned().map(|x| x.ks).collect::<Vec<f64>>(); // clip to just ks values
        let mut least = 1.0; // test for lowest fit (set to high value)
        let mut low = Reservoir::new(); // initialize variable to hold lowest fit
        for (i, val) in ns.iter().enumerate() {
            let loss = ks[i] + val; // loss function
            if loss < least {
                // if lowest value
                low = res[i].clone(); // copy to low
                least = loss; // set least to new low value
            }
        }

        low.mass
    }

    /// Returns raw transit times based on model parameters.
    pub fn bulk_transits(self) -> Vec<f64> {
        let res = self
            .clone()
            .model
            .seed_clones()
            .iter()
            .map(|x| self.clone().model(x).sim().mass)
            .collect::<Vec<Vec<f64>>>();
        let mut mass = Vec::new();
        for mut r in res {
            mass.append(&mut r);
        }
        mass
    }

    /// Return CDF of transit times based on model parameters.
    pub fn transit_times(self) -> Vec<f64> {
        let res = self
            .clone()
            .model
            .seed_clones()
            .iter()
            .map(|x| self.clone().model(x).sim())
            .collect::<Vec<Reservoir>>();

        let index = self.model.index;
        let mut out = vec![Complex::new(0.0, 0.0); index.len()];
        let mut real_planner = RealFftPlanner::<f64>::new();
        info!("Constructing FftPlanner for fourier transforms.");
        let r2c = real_planner.plan_fft_forward(index.len() + 1);
        for r in res {
            let cdf = utils::cdf_rng(&r.mass, &index);
            let mut pmf = utils::pmf_from_cdf(&cdf);
            let mut spectrum = r2c.make_output_vec();
            info!("Forward transforming the pmf using fft.");
            r2c.process(&mut pmf, &mut spectrum).unwrap();
            info!("Summing transformed pmfs.");
            out = out
                .iter()
                .zip(spectrum.iter())
                .map(|(a, b)| a + b)
                .collect::<Vec<Complex<f64>>>();
        }
        info!("Constructing FftPlanner for inverse fourier transforms.");
        let c2r = real_planner.plan_fft_inverse(index.len() + 1);
        info!("Inverse fourier transform using fft.");
        let mut out_data = c2r.make_output_vec();
        c2r.process(&mut out, &mut out_data).unwrap();
        info!("Normalize output by dividing by length.");
        out_data = out_data
            .par_iter()
            .map(|a| a / index.len() as f64)
            .collect::<Vec<f64>>();
        info!("Normalize output by dividing by sum of pmfs (having added several pmfs together).");
        let sum_out = out_data.iter().fold(0.0, |acc, x| acc + *x);
        out_data = out_data
            .par_iter()
            .map(|a| a / sum_out)
            .collect::<Vec<f64>>();

        out_data
    }
}

impl Default for Reservoir {
    fn default() -> Self {
        Self::new()
    }
}

/// Holder struct to read in charcoal sample ages from csv.
#[derive(Debug, Deserialize)]
pub struct Sample {
    id: String,
    /// Mean estimated charcoal age in years before 2000.
    pub age: f64,
    /// Deposit type - either debris flows (DF), fluvial fines (FF) or fluvial gravels (FG).
    pub facies: String,
}

impl Sample {
    /// Converts a csv file of charcoal ages into a Sample struct.
    pub fn read(path: &str) -> Result<Vec<Sample>, errors::ResError> {
        let mut record = Vec::new();
        let var = std::fs::File::open(path)?;
        let mut rdr = csv::Reader::from_reader(var);
        for result in rdr.records() {
            let row = result?;
            let row: Sample = row.deserialize(None)?;
            record.push(row);
        }
        Ok(record)
    }
}

/// Thresholds for statistical tests.
#[derive(Clone, Debug)]
pub struct Thresholds {
    ad: f64,
    ch: f64,
    kp: f64,
    ks: f64,
}

impl Thresholds {
    /// Builder for threshold struct.
    pub fn new(ad: f64, ch: f64, kp: f64, ks: f64) -> Thresholds {
        Thresholds { ad, ch, kp, ks }
    }
}

/// Struct to hold statistics describing transit times of reservoirs.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Transits {
    rate: f64,
    mean: f64,
}

impl Transits {
    /// Convert csv record to Transits struct.
    pub fn read(path: &str) -> Result<Vec<Transits>, errors::ResError> {
        let mut rec = Vec::new();
        let var = std::fs::File::open(path)?;
        let mut rdr = csv::Reader::from_reader(var);
        for result in rdr.records() {
            let row = result?;
            let row: Transits = row.deserialize(None)?;
            rec.push(row);
        }
        Ok(rec)
    }
}

/// The Record trait indicates the data is organized in spreadsheet format with
/// variables by column and observations by row (using struct fields as column names).
/// Trait methods provides convenience functions for wrangling spreadsheet data.
pub trait Record {
    /// Sorts data and divides into bins, returning averages by bin.
    fn bin_ave(&self, bins: usize) -> (Vec<f64>, Vec<f64>);
}

impl Record for Vec<Gof> {
    /// Orders of vector of Gof structs by input rate,
    /// breaks into chunks based on the number of bins desired `bins`,
    /// returns the mean input rate and ks fit per bin.
    fn bin_ave(&self, bins: usize) -> (Vec<f64>, Vec<f64>) {
        let ord = &mut (*self).clone();
        ord.sort_by(|x, y| x.input.partial_cmp(&y.input).unwrap());
        let chunk_ln = (ord.len() as f64 / bins as f64).round() as usize;
        let rates: Vec<f64> = ord.iter().map(|x| x.input).collect();
        let rates: Vec<f64> = rates.chunks(chunk_ln).map(|x| utils::mean(x)).collect();
        let fits: Vec<f64> = ord.iter().map(|x| x.ks).collect();
        let fits: Vec<f64> = fits.chunks(chunk_ln).map(|x| utils::mean(x)).collect();
        (rates, fits)
    }
}