## add lead & lag discharge to main dataframe
function get_leadlag(times::Vector{DateTime}, df::DataFrame, x::String)
   for (i,j) in enumerate(times)
      for (ind,val) in enumerate( filter(row -> j-Dates.Hour(2) .<= row.datetime .<= j+Dates.Hour(2), df)[!,Symbol(x)] )
         bel[i, Symbol(string(x, ind))] += val
      end
   end
end

## Function to impute missing value sin dataframe
function impute_var(df::DataFrame, x::String)
   for i in eachrow(df)
      for j in 1:20
         if i[string(x,j)] == 0
            i[string(x,j)] += i[string(x,j-1)]
         end
      end
   end
end


## Get site input
function get_site_input()
        println("Please enter a site  04168557 or 04165705")
        site = readline()
        if contains(site, r"04168557|04165705") == false
           println("Please input site 04168557 or 04165705")
           site = get_site_input()
        end
return(site)
end

## Get date Time input
function get_dt_input()
        println("Please enter date and time you would like to predict  YYYY-MM-DDTHH:MM:SS")
        dt = readline()
        try
          dt =  DateTime(dt)
        catch e
           println("Please use this format: YYYY-MM-DDTHH:MM:SS")
           dt = get_dt_input()
        end
return(dt)
end

## NWIS URL
function nwis_url(begin_date::DateTime, end_date::Date=Date(begin_date))
   begin_date = Date(begin_date)
   string("https://nwis.waterdata.usgs.gov/nwis/uv?cb_00060=on&cb_00065=on&cb_72255=on&format=rdb&site_no=04165710&period=&begin_date=",begin_date,"&end_date=", end_date)
end

## Get continous data from NWIS Fort Wayne Detroit
function nwis_pull(dt::DateTime)
        url = nwis_url(dt)
        cont_dis = CSV.read(download(url),DataFrame,comment="#", delim="\t")
        deleteat!(cont_dis,1)
        dropmissing!(cont_dis)
return(cont_dis)
end

## Clean Continous Discharge
function clean_dis!(cont_dis::DataFrame=cont_dis)
   rename!(cont_dis, Symbol("240039_00060") => :discharge,
                  Symbol("72269_00065") => :gage_height,
                 Symbol("242971_72255") => :channel_velocity)
   select!(cont_dis, [:datetime, :discharge, :gage_height, :channel_velocity])
   cont_dis.datetime = DateTime.(cont_dis.datetime, dateformat"yyyy-mm-dd HH:MM:SS")
   cont_dis.discharge =  parse.(Int64, cont_dis.discharge)
   for i in [:gage_height, :channel_velocity]
        cont_dis[!,i] =  parse.(Float64, cont_dis[!,i])
   end
return(cont_dis)
end

## Make lead and lag dataframe columns
function make_lead_lag!(bel::DataFrame)
   ## Insert cols for lead and lag of variables
   insertcols!(bel, (Symbol.(string.("discharge", 1:21)) .=> 0)...)
   for i in ["gage_height", "channel_velocity"]
        insertcols!(bel, (Symbol.(string.(i, 1:21)) .=> 0.0)...)
   end
end

## Fill in lead and lag values from continous data
function fill_lead_lag!(bel::DataFrame, cont_dis::DataFrame=cont_dis)
   ## Add lead lag of values from continous discharge to compute current discharge
   for i in ["discharge", "gage_height", "channel_velocity"]
        get_leadlag(bel.datetime, cont_dis, i)
   end
   select!(bel, Not(findall(c -> occursin("21",c), names(bel))) )
end

## Impute Zero or missing values
function impute_z!(bel::DataFrame)
   ## Impute zeros or remove instances with all zeros
   for i in ["discharge", "gage_height", "channel_velocity"]
      impute_var(bel, i)
   end
end

## Take the mean of travel time 1 Hour before
function get_lag(time::DateTime, cont_dis::DataFrame)
        rw = filter(row ->  time-Dates.Hour(2) .<= row.datetime .<= time-Dates.Hour(1) , cont_dis)[!,["datetime","discharge"]]
        round( sum(rw.discharge)/length(rw.discharge), digits=0)
end
