include("Catchment_1950-2022")
 using DelimitedFiles
 using BlackBoxOptim

 forcing = readdlm("forcing_1950-2022new.txt",Float64)#check
 Temp = readdlm("Temp_1950-2022.txt",Float64)
 # WE = readdlm("newelevation_weight.txt",Float64)
 # landscape_per = readdlm("Landscape_per.txt",Float64)
 # Pre_per = readdlm("prezone_percent.txt",Float64)
 H3Catpre_per=readdlm("prezone_percent.txt",Float64)
 H3Cat_WE=readdlm("newTriCat_elevation_weight.txt",Float64)
 H3Cat_lanper=readdlm("landscape_per.txt",Float64)
 #

 # forcing = forcing[4019:25202,:]
 # Temp= Temp[4019:25202,4:13]#1970-2001warmup，2001-2009 Cali

 Temp= Temp[:,4:13]#1970-2001warmup，2001-2009 Cali
  # forcing=forcing[1:15706,:]#1970-2001warmup，2001-2009 Cali

  # forcing=forcing[3288:17897,:]#1970-2001warmup，2001-2009 Cali
 #warm up 1978-1989[1:4383] Cali:1990-2009[4384:11688],Vali2010-2016[11689:14245]
 #warmup 1988-1989 [3653:4383]

 # Temp= Temp[1:9861,4:13]#1961-1977warmup，1978-1987 Cali
 #
 # forcing=forcing[1:9861,:]#1961-1977warmup，1978-1987 Cali

 # Temp= Temp[3288:13514,4:13]#1970-1987warmup，1988-1997 Cali
 #
 # forcing=forcing[3288:13514,:]#1970-1987warmup，1988-1997 Cali
 # Temp= Temp[6940:17166,4:13]#1980-1997warmup，1998-2007 Cali
 #
 # forcing=forcing[6940:17166,:]#1980-1997warmup，1998-2007 Cali

 const Emax=10
  Par_fix=4000.0
 # const tmax=14610
 const delay=0.99984587
 #with SumaxCali_entire
   # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 157.873 3.42995 0.0894629 2.15433 0.413547 0.967392 94.5823 4.60649 0.867387 0.219394 60.8975 0.345032 1.04905]
 #with SumaxCali_P1
  # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 148.009 3.42995 0.0894629 2.15433 0.413547 0.967392 68.001 4.60649 0.867387 0.219394 54.9773 0.345032 1.04905]
 #with SumaxCali_P2
  # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 160.2380 3.42995 0.0894629 2.15433 0.413547 0.967392 75.1460 4.60649 0.867387 0.219394 58.6793 0.345032 1.04905]
 #with SumaxCali_P3
   # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 120.3060 3.42995 0.0894629 2.15433 0.413547 0.967392 93.1949 4.60649 0.867387 0.219394 51.2889 0.345032 1.04905]
 #with SumaxCali_P4
   # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 176.7880 3.42995 0.0894629 2.15433 0.413547 0.967392 133.2000 4.60649 0.867387 0.219394 34.2196 0.345032 1.04905]

   # the calibration parameter set(1953-2022)_with SumaxWB_entire
    # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 143.196 3.42995 0.0894629 2.15433 0.413547 0.967392 85.789 4.60649 0.867387 0.219394 55.236 0.345032 1.04905]
    #with SumaxWB_P1
  # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 129.83285 3.42995 0.0894629 2.15433 0.413547 0.967392 77.78334 4.60649 0.867387 0.219394 50.08137 0.345032 1.04905]
    #with SumaxWB_P2
 # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 156.45969 3.42995 0.0894629 2.15433 0.413547 0.967392 93.73558 4.60649 0.867387 0.219394 60.35233 0.345032 1.04905]
    #with SumaxWB_P3
 # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 129.74343 3.42995 0.0894629 2.15433 0.413547 0.967392 77.72978 4.60649 0.867387 0.219394 50.04688 0.345032 1.04905]
    #with SumaxWB_P4
    # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 136.679 3.42995 0.0894629 2.15433 0.413547 0.967392 81.885 4.60649 0.867387 0.219394 52.722 0.345032 1.04905]
    PFparset7= readdlm("PFsolutionobj_1953-2022(fixSsp)_addREnewprezone_narrow2.txt",Float64)
     # Result=Catchment_all(Par_fix,PFparset7[1609,:],forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
    tmax=length(forcing[:,1])
## change sumax==Sumax——entire
    # PFparset7[:,6].=143.196
    # PFparset7[:,12].=85.789
    # PFparset7[:,16].=55.236
## change sumax==Sumax——P1
    # PFparset7[:,6].=129.74343
    # PFparset7[:,12].=77.72978
    # PFparset7[:,16].=50.08137
## change sumax==Sumax——P2
    # PFparset7[:,6].=156.4596856
    # PFparset7[:,12].=93.7355781
    # PFparset7[:,16].=60.35233196
## change sumax==Sumax——P3
    PFparset7[:,6].=129.83285
    PFparset7[:,12].=77.78334
    PFparset7[:,16].=50.04688

## change sumax==Sumax——P4
   # PFparset7[:,6].=136.6785502
   # PFparset7[:,12].=81.88462649
   # PFparset7[:,16].=52.72201079


    # PF_E=zeros(tmax,length(PFparset7[:,1]))
    # PF_Ss=zeros(tmax,length(PFparset7[:,1]))
     # PF_H3=zeros(tmax,length(PFparset7[:,1]))
    # PF_fitness=zeros(length(PFparset7[:,1]),8)

    # PF_Melt=zeros(tmax,length(PFparset7[:,1]))
    # PF_Su=zeros(tmax,length(PFparset7[:,1]))

    PF_Su=zeros(tmax,length(PFparset7[:,1]))
    PF_Sf=zeros(tmax,length(PFparset7[:,1]))
    PF_Qf=zeros(tmax,length(PFparset7[:,1]))
    PF_Qs=zeros(tmax,length(PFparset7[:,1]))

    for i in 1:length(PFparset7[:,1])
         Par=PFparset7[i,:]

         Result=Catchment_all(Par_fix,Par,forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
         # for j in 1:8
         #     PF_fitness[i,j]=Result[j]
         # end
         PF_Su[:,i]=Result[1]
         PF_Sf[:,i]=Result[2]
         PF_Qf[:,i]=Result[3]
         PF_Qs[:,i]=Result[4]


     end

     writedlm("PF_Su_1950-2022(fixSsp)_addREnewprezone_narrow2_with_SumaxWB_t3_right.txt",PF_Su)
     writedlm("PF_Sf_1950-2022(fixSsp)_addREnewprezone_narrow2_with_SumaxWB_t3_right.txt",PF_Sf)
     writedlm("PF_Qf_1950-2022(fixSsp)_addREnewprezone_narrow2_with_SumaxWB_t3_right.txt",PF_Qf)
     writedlm("PF_Qs_1950-2022(fixSsp)_addREnewprezone_narrow2_with_SumaxWB_t3_right.txt",PF_Qs)
     # writedlm("PF_Melt_1950-2022(fixSsp)_addREnewprezone_narrow2_right.txt", PF_Melt)
     # writedlm("PF_Su_1950-2022(fixSsp)_addREnewprezone_narrow2_right.txt", PF_Su)

     # writedlm("PF_H3_1950-2022(fixSsp)_addREnewprezone_narrow2_right.txt",PF_H3)

     # writedlm("PF_Ss_1950-2022(fixSsp)_addREnewprezone_narrow2_with_SumaxWB_t4_right.txt",PF_Ss)
     # writedlm("PF_fitness_1950-2022(fixSsp)_addREnewprezone_narrow2.txt",PF_Qm_prezone2)
   #
   # result=Catchment_all(Par_fix,Par,forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
   # writedlm("Qm_1950-2022_balance_Cali_1953-2022_Narrowrange_with_SumaxWB_P4_right.txt",result[9])

#
 # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 157.873 3.42995 0.0894629 2.15433 0.413547 0.967392 94.5823 4.60649 0.867387 0.219394 60.8975 0.345032 1.04905]
#  Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 214.7227346 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 172.4925346 3.4709756	1.983677119	0.255845694	66.1313059	0.356505754	1.151475865]
# re=Catchment_all(Par_fix,Par,forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
# DE(obj)=sqrt(mean(abs2,obj[1:end-1]))
# DE(re)
 # Par_other=readdlm("PFsolutionobj_1953-1972_Cali_withoutH3(fixSsp)(long-warmup)newprezone.txt",Float64)
 # Qm=zeros(length(forcing[1:end,1]),1)
 # Par=Par_other[393,:]
 # result=Catchment_all(Par_fix,Par,forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
 # writedlm("Qm_1950-2022_balance_Cali_withoutH3(fixSsp)_addREnewprezone.txt",result[2])

 ## Tt Cmelt Ca Ks Ssp SimaxF BetaF DF CpmaxF KfF  SimaxG BetaG  CpmaxG KfG BetaW  CpmaxW
##Sumax fixed
   # Par_fix=[175.0 105.0 45.86]
  # Par_fix=[132.52 115.0 90.0]

  # Par=[-0.171034 2.20272 0.386386 0.0173267 4644.01 2.55644 0.0430876 0.371471 2.7973 0.2303 1.10066 4.72474 3.52366 0.387088 1.20678 0.0100404]
  # result=Catchment(Par_fix,Par,forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
 # -0.325290574	2.018749225	0.488431491	0.106538527	3791.730478	3.58024412	349.3300316	3.195166358	0.149916943	2.743015469	0.20864908	0.629232663	295.6320429	0.249027021	0.206212352	0.33413554	130.2539654	0.182804531	0.109930646

#-0.325290574	2.018749225	0.488431491	0.106538527	3791.730478	3.58024412	349.3300316	3.195166358	0.149916943	2.743015469	0.20864908	0.629232663	295.6320429	0.249027021	0.206212352	0.33413554	130.2539654	0.182804531	0.109930646
# 1 0.25 0.022 2500.0 3.69 480.6 1.88 0.19 0.34 0.61 1.23 296.0 1.28 2.59 0.34 146.3 0.122 0.289]


 Pro(Par)=Catchment_all(Par_fix,Par,forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
 All_parameters = Vector{Float64}[]
 All_fitness =[]
 function my_saving_fitness(Par)
      global All_parameters
      push!(All_parameters, deepcopy(Par))
      f = Pro(Par)
      push!(All_fitness, f)
      return f
end
#the Prior distribution
# SearchRange=[(-2.5,2.5),(1.0,5.0),(0.1,0.7),(0.001,0.2),(0.0,10000.0),
# (1.5,5.0),(300.0,500.0),(0.1,5.0),(0.0,1.0),(0.1,4.0),(0.2,5.0),
# (0.1,1.5),(150.0,300.0),(0.1,5.0),(0.1,4.0),(0.2,5.0),
# (50.0,150.0),(0.1,5.0),(0.1,4.0)],
DE(obj)=sqrt(mean(abs2,obj[1:end-1]))
# DE(obj)=sqrt(mean(abs2,obj))
 res = bboptimize(my_saving_fitness; Method=:borg_moea,
                 FitnessScheme=ParetoFitnessScheme{8}(is_minimizing=true,aggregator=DE),
                 SearchRange=[(-2.5,2.5),(1.0,5.0),(0.1,0.7),(0.002,0.2),
                 (1.5,5.0),(80.0,180.0),(0.0,5.0),(0.0,1.0),(0.1,4.0),(0.2,5.0),
                 (0.0,1.5),(50.0,150.0),(0.1,5.0),(0.1,4.0),(0.2,5.0),
                 (5.0,75.0),(0.01,5.0),(0.01,4.0)],
                 PopulationSize=50,NumDimensions=18,ϵ=0.001,
                 MaxFuncEvals=15000,TraceInterval=1.0,TraceMode=:compact)
    # num_alls=length(All_parameters)
    # num_unique=length(unique( All_parameters))
    pf_solutions = map(params, pareto_frontier(res));
    pf_fitnesses = map(fitness, pareto_frontier(res));
     pf_S=zeros(length(pf_solutions),18)
     pf_F=zeros(length(pf_fitnesses),length(pf_fitnesses[1]))
     All_S=zeros(length(All_parameters),18)
     All_F=zeros(length(All_fitness),length(All_fitness[1]))
     #write the pareto front solutions and fitnesses
 for i in 1:length(pf_solutions)
     for j in 1:18
         pf_S[i,j]=pf_solutions[i][j]
     end
 end
 for m in 1:length(pf_fitnesses)
     for n in 1:length(pf_fitnesses[m])
         pf_F[m,n]=pf_fitnesses[m][n]
     end
 end
 #write all solutions and fitness
 for k in 1:length(All_parameters)
     for l in 1:18
         All_S[k,l]=All_parameters[k][l]
     end
 end
 for h in 1:length(All_fitness)
     for x in 1:length(All_fitness[h])
         All_F[h,x]=All_fitness[h][x]
     end
 end

 writedlm("PFsolutionobj_1953-2022(fixSsp)_addREnewprezone_narrow2.txt",pf_S)#SumaxF ,sumaxG ,SumaxW  0-200
 writedlm("PFfitnessobj_1953-2022(fixSsp)_addREnewprezone_narrow2.txt", pf_F)
 writedlm("all_Sobj_1953-2022(fixSsp)_addREnewprezone_narrow2.txt",All_S)
 writedlm("all_Fobj_1953-2022(fixSsp)_addREnewprezone_narrow2.txt",All_F)

# writedlm("PFsolutionobj(new6_H3)_meanmonthly_1998-2007_newRange.txt",pf_S) #SumaxF 150-250,sumaxG 75-150,SumaxW0-75
# writedlm("PFfitnessobj(new6_H3)_meanmonthly_1998-2007_newRange.txt", pf_F)
# writedlm("all_Sobj(new6_H3)_meanmonthly_1998-2007_newRange.txt",All_S)
# writedlm("all_Fobj(new6_H3)_meanmonthly_1998-2007_newRange.txt",All_F)

 # writedlm("PFsolutionobj(new8another_update).txt",pf_S)
 # writedlm("PFfitnessobj(new8another_update).txt", pf_F)
 # writedlm("all_Sobj(new8another_update).txt",All_S)
 # writedlm("all_Fobj(new8another_update).txt",All_F)


 #x=0.2*length(forcing[:,5])
 #y=0.7*length(forcing[:,5])
  #z=0.5*length(forcing[:,5])
  #m=0.1*length(forcing[:,5])

 #ParCat = [Tt Cmelt Ca Ks K_sas Ss_p]6(1:6)
 #ParForest = [SimaxF SumaxF betaF DF cpmaxF KfF]6(7:12)
 #ParGrassland=[SimaxG SumaxG betaG cpmaxG KfG]5(13:17)
 #ParWetland=[SumaxW betaW crmaxW KfW]4(18:21)
 #
 # Par=[0.75 4.71 0.25 0.022  1.0 3000 3.69 391.9 1.88 0.19 0.34 0.61 1.23 165.3 1.28 2.59 0.34 140.3 0.122 0.289 2.33 ]
 # result=Catchment(Par,forcing,Temp,WE,landscape_per,Pre_per)
