using Bigleaf
using Test, StableRNGs
using Pipe: @pipe 
using DataFrames, Dates, TimeZones
using Tables: columntable
using StaticArrays
using Statistics, StatsBase
using Missings

isapproxm(args...; kwargs...) = isapprox(args...; kwargs...); 
isapproxm(::Missing, ::Missing; kwargs...) = true

tha48 = DataFrame(
    datetime = ZonedDateTime("2014-06-01T00:00:00+00:00","yyyy-mm-ddTHH:MM:SSzzzz") .+ 
        (0:47) .* Minute(30),
    ustar = Union{Missing, Float64}[0.5400000214576721, 0.4900000095367432, 0.4799999892711639, 0.449999988079071, 0.5099999904632568, 0.4600000083446503, 0.5400000214576721, 0.5199999809265137, 0.4600000083446503, 0.4099999964237213, 0.5299999713897705, 0.5099999904632568, 0.5199999809265137, 0.4099999964237213, 0.5400000214576721, 0.4600000083446503, 0.5099999904632568, 0.5, 0.6299999952316284, 0.5799999833106995, 0.6800000071525574, 0.6499999761581421, 0.7400000095367432, 0.7300000190734863, 0.7699999809265137, 0.7400000095367432, 0.7799999713897705, 0.8700000047683716, 0.7799999713897705, 0.6700000166893005, 0.5600000023841858, 0.6600000262260437, 0.7099999785423279, 0.6100000143051147, 0.5899999737739563, 0.6100000143051147, 0.5299999713897705, 0.449999988079071, 0.3400000035762787, 0.3300000131130219, 0.3300000131130219, 0.3700000047683716, 0.3799999952316284, 0.3799999952316284, 0.3600000143051147, 0.3700000047683716, 0.3600000143051147, 0.3400000035762787],
    Tair = [11.88000011444092, 11.67000007629395, 11.1899995803833, 10.80000019073486, 10.67000007629395, 10.13000011444092, 9.779999732971191, 9.5, 9.09000015258789, 8.800000190734863, 8.6899995803833, 8.899999618530273, 9.430000305175781, 9.710000038146973, 10.55000019073486, 11.19999980926514, 11.72999954223633, 12.68000030517578, 13.1899995803833, 13.46000003814697, 14.1899995803833, 14.73999977111816, 14.65999984741211, 14.8100004196167, 15.02999973297119, 14.98999977111816, 14.77999973297119, 15.35000038146973, 15.5, 15.61999988555908, 15.47000026702881, 16.20000076293945, 16.13999938964844, 15.56999969482422, 15.14999961853027, 15.18000030517578, 14.81999969482422, 14.60000038146973, 14.10000038146973, 13.23999977111816, 12.65999984741211, 12.46000003814697, 12.09000015258789, 11.85999965667725, 11.72000026702881, 11.64000034332275, 11.52999973297119, 11.43000030517578],
    pressure = [97.63999938964844, 97.62999725341797, 97.61000061035156, 97.61000061035156, 97.61000061035156, 97.61000061035156, 97.61000061035156, 97.61000061035156, 97.62999725341797, 97.66000366210938, 97.66999816894531, 97.69000244140625, 97.69000244140625, 97.69999694824219, 97.70999908447266, 97.69999694824219, 97.7300033569336, 97.72000122070312, 97.70999908447266, 97.69999694824219, 97.69999694824219, 97.69999694824219, 97.69999694824219, 97.70999908447266, 97.70999908447266, 97.72000122070312, 97.70999908447266, 97.70999908447266, 97.69999694824219, 97.68000030517578, 97.68000030517578, 97.66999816894531, 97.68000030517578, 97.66999816894531, 97.66999816894531, 97.66000366210938, 97.6500015258789, 97.63999938964844, 97.63999938964844, 97.6500015258789, 97.66000366210938, 97.66999816894531, 97.69000244140625, 97.68000030517578, 97.68000030517578, 97.69000244140625, 97.69000244140625, 97.69000244140625],
    H = Union{Missing, Float64}[-68.18000030517578, -48.54000091552734, -59.09999847412109, -60.11000061035156, -59.40000152587891, -48.91999816894531, -53.52999877929688, -40.75, -29.28000068664551, -33.36999893188477, -9.050000190734863, 2.400000095367432, 38.29999923706055, 38.45000076293945, 85.08000183105469, 108.7799987792969, 176.6799926757812, 210.6900024414062, 230.0800018310547, 227.4199981689453, 321.6499938964844, 358.8999938964844, 329.9299926757812, 317.3099975585938, 375.1900024414062, 336.489990234375, 260.7099914550781, 360.6400146484375, 264.2200012207031, 185.3200073242188, 123.7699966430664, 236.0200042724609, 207.5200042724609, 128.3399963378906, 66.05999755859375, 79.6500015258789, 44.93999862670898, 16.22999954223633, -22.35000038146973, -29.10000038146973, -47.79999923706055, -63.61999893188477, -61.38000106811523, -65.11000061035156, -59.77000045776367, -60.52000045776367, -49.02999877929688, -53.45000076293945],
    LE = Union{Missing, Float64}[9.9399995803833, 5.269999980926514, 3.980000019073486, missing, missing, missing, missing, missing, 5.190000057220459, 10.11999988555908, 26.27000045776367, 27.3700008392334, 23.52000045776367, 6.639999866485596, 52.2599983215332, 54.02000045776367, 92.27999877929688, 100.9199981689453, 111.7200012207031, 223.1000061035156, 200.7400054931641, 185.0500030517578, 170.9799957275391, 183.4900054931641, 187.6900024414062, 223.0299987792969, 161.6600036621094, 160.9700012207031, 153.7599945068359, 77.20999908447266, 118.4300003051758, 31.86000061035156, 116.4100036621094, 84.98999786376953, 62.2400016784668, 68.12999725341797, 29.88999938964844, 41.65000152587891, 28.77000045776367, 16.53000068664551, 16.04000091552734, 10.67000007629395, 2.880000114440918, 5.369999885559082, 1.820000052452087, 1.580000042915344, 4.139999866485596, 4.880000114440918],
    Rn = [-86.48999786376953, -84.19999694824219, -81.80999755859375, -77.9000015258789, -79.08999633789062, -79.08999633789062, -80.13999938964844, -73.36000061035156, -66.63999938964844, -37.93999862670898, -25.25, 78.0, 113.2399978637695, 121.9499969482422, 230.6199951171875, 302.1700134277344, 394.5400085449219, 453.8800048828125, 518.2100219726562, 609.9000244140625, 693.4099731445312, 729.1400146484375, 767.3599853515625, 778.1699829101562, 778.5599975585938, 778.239990234375, 606.7899780273438, 724.239990234375, 524.8499755859375, 425.1499938964844, 399.75, 515.6400146484375, 371.9100036621094, 304.1300048828125, 161.6799926757812, 176.8000030517578, 108.5299987792969, 35.54999923706055, -25.29999923706055, -72.19999694824219, -91.4800033569336, -92.30000305175781, -92.12000274658203, -90.70999908447266, -90.12000274658203, -89.77999877929688, -87.5, -86.76000213623047],
    G = [-4.934999942779541, -5.085000038146973, -5.135000228881836, -5.210000038146973, -5.28000020980835, -5.34499979019165, -5.5, -5.804999828338623, -6.239999771118164, -6.550000190734863, -6.385000228881836, -6.039999961853027, -5.034999847412109, -4.070000171661377, -3.019999980926514, -1.475000023841858, 0.5550000071525574, 1.534999966621399, 2.630000114440918, 7.550000190734863, 22.06500053405762, 17.09499931335449, 18.93000030517578, 15.5649995803833, 16.90500068664551, 12.55000019073486, 30.14999961853027, 23.71500015258789, 14.81999969482422, 15.06999969482422, 9.925000190734863, 6.525000095367432, 4.864999771118164, 5.394999980926514, 4.920000076293945, 3.289999961853027, 2.345000028610229, 1.559999942779541, 0.6499999761581421, -0.4550000131130219, -1.669999957084656, -2.795000076293945, -3.674999952316284, -4.295000076293945, -4.775000095367432, -5.190000057220459, -5.364999771118164, -5.440000057220459],
    VPD = [0.5745999813079834, 0.5633999824523925, 0.513700008392334, 0.4560999870300293, 0.4184000015258789, 0.3608999967575073, 0.3033999919891358, 0.2822999954223633, 0.2641999959945679, 0.2621000051498413, 0.2773000001907349, 0.3075999975204468, 0.3339999914169312, 0.3270999908447266, 0.373799991607666, 0.4267000198364258, 0.4580999851226807, 0.5138000011444092, 0.5421999931335449, 0.6954999923706054, 0.8619000434875488, 1.010499954223633, 1.014999961853027, 1.075800037384033, 1.090100002288818, 1.094499969482422, 0.9982000350952148, 1.085700035095215, 1.094299983978271, 1.09379997253418, 1.096599960327148, 1.164799976348877, 1.048600006103516, 0.9133999824523926, 0.8229999542236328, 0.8409000396728515, 0.7889999866485595, 0.7486999988555908, 0.6928999900817872, 0.601800012588501, 0.5798999786376953, 0.5870999813079834, 0.5499000072479248, 0.5432000160217285, 0.5427000045776367, 0.5219999790191651, 0.5199999809265137, 0.511299991607666],
    wind = Union{Missing, Float64}[4.210000038146973, 4.460000038146973, 4.539999961853027, 4.079999923706055, 3.950000047683716, 4.019999980926514, 3.269999980926514, 3.480000019073486, 3.240000009536743, 3.420000076293945, 3.440000057220459, 2.970000028610229, 3.349999904632568, 2.660000085830688, 2.779999971389771, 2.329999923706055, 2.220000028610229, 2.160000085830688, 2.519999980926514, 2.980000019073486, 2.359999895095825, 2.420000076293945, 2.920000076293945, 3.359999895095825, 2.759999990463257, 3.279999971389771, 3.410000085830688, 3.480000019073486, 3.039999961853027, 3.950000047683716, 2.700000047683716, 2.829999923706055, 2.730000019073486, 3.640000104904175, 2.75, 3.299999952316284, 2.630000114440918, 2.049999952316284, 1.879999995231628, 2.130000114440918, 2.400000095367432, 2.619999885559082, 2.640000104904175, 2.589999914169312, 2.589999914169312, 2.819999933242798, 2.779999971389771, 2.660000085830688],
    #
    # from Su2001 of entire tha-dataset (see tmpf below)
    Gb_h = Union{Missing, Float64}[0.1822661462615249, 0.1750029796115819, 0.16439640538330097, 0.15115204867322993, 0.17536657313260987, 0.1625005163806663, 0.18840537219804904, 0.18879242029415302, 0.17645201287966256, 0.1538975543389697, 0.21088990351067222, 0.2159981919177606, 0.26124601608024695, 0.2773512635093451, 0.31590536172758477, 0.40377826077805495, 0.4767947347202397, 0.56854692623082, 0.42750700682547943, 0.4686532838500691, 0.4713224944023968, 0.5367616432054471, 0.4390590733192645, 0.4365982669032558, 0.45028995760447604, 0.44342264360628164, 0.3870369356275578, 0.4091200789143718, 0.38965757992457295, 0.3741648297018856, 0.3598397227673043, 0.41828517646506513, 0.37765531002288344, 0.346427027859903, 0.2926818717409249, 0.3033011874663078, 0.2735927127688745, 0.23189051804555683, 0.13765984163452955, 0.1230228546581407, 0.10194295317288511, 0.11062842016540558, 0.11745785563008848, 0.11461165845075606, 0.107937932508708, 0.11269011080949713, 0.11690251453131727, 0.1020485594839973],
    Ga_h = Union{Missing, Float64}[0.050190555728311555, 0.04116957600962841, 0.03877814728450016, 0.03736361775914192, 0.047872524670215494, 0.03975837173989957, 0.06052647712603172, 0.05504593321238934, 0.04766632363024636, 0.03725385088675965, 0.05886451561135729, 0.06231168613359922, 0.06166420574946217, 0.05146825750242876, 0.07874566459614012, 0.07414026084897143, 0.09405110045117922, 0.09616429374872118, 0.1150966610946552, 0.09097297096976756, 0.13839881444493318, 0.1317378026984531, 0.13140678312765053, 0.11633916710154416, 0.1454360006411824, 0.12128624346147493, 0.12212101975187602, 0.14200569128979465, 0.1322214598437316, 0.0871694750533624, 0.08780626502144728, 0.11251759597304214, 0.12401547627348973, 0.07893328170976276, 0.08836491703204545, 0.08219874492644172, 0.07681770620097074, 0.06927204867424476, 0.042503890501350394, 0.036116991360095725, 0.03139917116540786, 0.03548953025999413, 0.03731866634395528, 0.03750740234735876, 0.034189028583981027, 0.03392950934600214, 0.03332805495857941, 0.030478849592838437],
);

i_tmp = () -> begin 
    Dl=0.01
    df = aerodynamic_conductance!(copy(df, copycols=false); Gb_model=Su2001(), 
        Dl, LAI=thal.LAI, zh=thal.zh, zr=thal.zr);
    @show df.Gb_h[1:48];
    @show df.Ga_h[1:48];
end



thal = (
    LAI = 7.6,   # leaf area index
    zh  = 26.5,  # average vegetation height (m)
    zr  = 42,    # sensor height (m)
    Dl  = 0.01,  # leaf characteristic dimension (m)
)


@testset "Bigleaf" begin
    @testset "bigleaf_constants" begin
        include("bigleaf_constants.jl")
    end
    @testset "util" begin
        include("util.jl")
    end
    @testset "unit_conversions" begin
        include("unit_conversions.jl")
    end
    @testset "filter_data" begin
        include("filter_data.jl")
    end
    @testset "meteorological_variables" begin
        include("meteorological_variables.jl")
    end
    @testset "sun_position" begin
        include("sun_position.jl")
    end
    @testset "potential_radiation" begin
        include("potential_radiation.jl")
    end
    @testset "stability_correction" begin
        include("stability_correction.jl")
    end
    @testset "surface_roughness" begin
        include("surface_roughness.jl")
    end
    @testset "boundary_layer_conductance" begin
        include("boundary_layer_conductance.jl")
    end
    @testset "aerodynamic_conductance" begin
        include("aerodynamic_conductance.jl")
    end
    @testset "surface_conductance" begin
        include("surface_conductance.jl")
    end
    @testset "evapotranspiration" begin
        include("evapotranspiration.jl")
    end
end
