include("Catch_1973-1992")
 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)

 # Temp= Temp[1:8401,4:13]#1950-1952warmup，1953-1972 Cali
 # forcing=forcing[1:8401,:]#1950-1952warmup，1953-1972 Cali

 Temp= Temp[4019:15706,4:13]#1961-1973warmup，1973-1992 Cali
 forcing=forcing[4019:15706,:]#1961-1973warmup，1973-1992 Cali
 #
 # Temp= Temp[10228:23011,4:13]#1978-1993warmup，1993-2012 Cali
 # forcing=forcing[10228:23011,:]#1978-1993warmup，1993-2012 Cali
 #
 # Temp= Temp[17533:end,4:13]#1998-2013warmup，2013-2022 Cali
 # forcing=forcing[17533:end,:]#1998-2013warmup，2013-2022 Cali

 const Emax=10
 # const tmax=14610
 const delay=0.99984587

  Par_fix=4000.0

  # the calibration parameter set(1953-2022)
   # 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]
  # 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]

  ##(Sumax_WB claculated based on percent of SumaxCali_entireCali)

  #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]

  ##(Sumax_WB claculated based on percent of SumaxCali_indivdual Cali)

  #with SumaxWB_P1
    # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 143.68608 3.42995 0.0894629 2.15433 0.413547 0.967392 66.01489 4.60649 0.867387 0.219394 53.37157 0.345032 1.04905]
  #with SumaxWB_P2
   # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 172.07556 3.42995 0.0894629 2.15433 0.413547 0.967392 80.69740 4.60649 0.867387 0.219394 63.01423 0.345032 1.04905]
  #with SumaxWB_P3
   # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 115.37847 3.42995 0.0894629 2.15433 0.413547 0.967392 89.37779 4.60649 0.867387 0.219394 49.18819 0.345032 1.04905]
  #with SumaxWB_P4
   # Par=[0.401069 4.4582 0.664156 0.0319121 1.54687 175.76288 3.42995 0.0894629 2.15433 0.413547 0.967392 132.42763 4.60649 0.867387 0.219394 34.02118 0.345032 1.04905]

  ##with SumaxCali
  #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]

  result=Catchment_second(Par_fix,Par,forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
  writedlm("Qm_1973-1992_balance_Cali_1953-2022_Narrowrange_with_SumaxWB_P4_right.txt",result[9])

# the calibration parameter set(1953-2022)
# 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]

# the calibration parameter set(1953-2022)_with change Sumax_WB_Period2
# Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 157.6565823 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 130.0382716 3.4709756	1.983677119	0.255845694	45.77172119 0.356505754	1.151475865]

# the calibration parameter set(1953-2022)_with change sumax basedon indiviCali
# Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 223.37 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 184.24 3.4709756	1.983677119	0.255845694	64.85 0.356505754	1.151475865]

# the calibration parameter set(1953-2022)_with change entire sumax basedon WB
# Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 135.5821943 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 108.9166016 3.4709756	1.983677119	0.255845694	41.75694163 0.356505754	1.151475865]

# the calibration parameter set(1953-2022)_with change Sumax_WB_Period1
 # Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 180.1788053 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 70.89617113 3.4709756	1.983677119	0.255845694	44.28666245 0.356505754	1.151475865]

 # the calibration parameter set(1953-2022)_with Sumax_WB_Period3
# Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 169.7146091 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 62.88977234 3.4709756	1.983677119	0.255845694	52.58648037 0.356505754	1.151475865]

# the calibration parameter set(1953-2022)_with with Sumax_WB_Period4
# Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 150.3235211 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 134.7343891 3.4709756	1.983677119	0.255845694	39.49688721 0.356505754	1.151475865]

# the calibration parameter set(1953-2022)_with change Sumax_Cali_Period1
# Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 230.56 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 90.72 3.4709756	1.983677119	0.255845694	56.67 0.356505754	1.151475865]

# the calibration parameter set(1953-2022)_with Sumax_Cali_period3
# Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 167.015 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 61.8894 3.4709756	1.983677119	0.255845694	51.75 0.356505754	1.151475865]

# the calibration parameter set(1953-2022)_with Sumax_Cali_period4
# Par=[0.108841363 2.623800609 0.57336599 0.069703276 3.566402859 213.155 2.744307011 0.151188493 2.544796441 2.844668237 0.862398923 191.05 3.4709756	1.983677119	0.255845694	56.0056 0.356505754	1.151475865]


 # the calibration parameter set(1973-1992)
# Par=[0.566742935	2.703719557	0.65783071	0.121726816	4.338403446	157.6565823	2.358378419	0.204824296	1.734947449	0.339112194	1.139862317	130.0382716	1.604360949	1.728852398	0.229433377	45.77172119	0.272228736	0.458295214]

# the calibration parameter set(1973-1992)_with change sumax basedon individual calibration
# Par=[0.566742935	2.703719557	0.65783071	0.121726816	4.338403446	223.37 	2.358378419	0.204824296	1.734947449	0.339112194	1.139862317	184.24 	1.604360949	1.728852398	0.229433377	64.85 	0.272228736	0.458295214]

    # the calibration parameter set(1993-2012)
  # Par=[-0.710800646	3.130758749	0.399306804	0.023110957	3.7988819	167.0151462	3.855003387	0.177634004	3.700813422	3.362787571	1.398241982	61.8894231	3.328103489	3.408419547	0.239366194	51.75433588	2.080083108	1.147691592]
  # the calibration parameter set(1993-2012)_with change sumax basedon WB
  # Par=[-0.710800646 3.130758749	0.399306804	0.023110957	3.7988819	157.6565823	3.855003387	0.177634004	3.700813422	3.362787571	1.398241982	130.0382716	3.328103489	3.408419547	0.239366194	45.77172119	2.080083108	1.147691592]

  # the calibration parameter set(1993-2012)_with change sumax basedon individual Cali
  # Par=[-0.710800646 3.130758749	0.399306804	0.023110957	3.7988819	223.37 	3.855003387	0.177634004	3.700813422	3.362787571	1.398241982	184.24  3.328103489	3.408419547	0.239366194	64.85 	2.080083108	1.147691592]

# the calibration parameter set(2013-2022)
# Par=[1.864866782 1.304194509	0.437674884	0.022656487	3.035483045	213.1548911	1.713106864	0.797174543	2.891998544	0.374855918	0.819007823	191.0496264	4.19623277	2.735724677	0.256815477	56.00558056	0.074060751	0.86410777]

# the calibration parameter set(2013-2022)_with change sumax basedon WB
# Par=[1.864866782 1.304194509	0.437674884	0.022656487	3.035483045	157.6565823	1.713106864	0.797174543	2.891998544	0.374855918	0.819007823	130.0382716	4.19623277	2.735724677	0.256815477	45.77172119	0.074060751	0.86410777]

# the calibration parameter set(2013-2022)_with change sumax basedon individual Cali
# Par=[1.864866782 1.304194509	0.437674884	0.022656487	3.035483045	223.37	1.713106864	0.797174543	2.891998544	0.374855918	0.819007823	184.24	4.19623277	2.735724677	0.256815477	64.85	0.074060751	0.86410777]

  # Par_other=readdlm("PFsolutionobj_1973-1992_Cali_withoutH3(fixSsp)(addRE)newpreone.txt",Float64)
  # Qm=zeros(length(forcing[1:end,1]),1)
  #  # Par=Par_other
  # # Par=Par_other[1564,:]
  # result=Catchment_second(Par_fix,Par,forcing,Temp,H3Cat_WE,H3Cat_lanper,H3Catpre_per)
  # writedlm("Qm_1961-1992_balance_withoutH3(fixSsp)newprezone_Cali_1953-2022with_SumaxCali_Period4.txt",result[9])



 Pro(Par)=Catchment_first(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[1:end-1])+obj[end]^2)/2)
 # DE(obj)=sqrt(mean(abs2,obj))
 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{6}(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),(100.0,250.0),(0.0,5.0),(0.0,1.0),(0.1,4.0),(0.2,5.0),
                 (0.0,1.5),(50.0,200.0),(0.1,5.0),(0.1,4.0),(0.2,5.0),
                 (5.0,75.0),(0.0,5.0),(0.0,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_1973-1992_Cali_withoutH3(fixSsp)_right.txt",pf_S)#SumaxF ,sumaxG ,SumaxW  0-200
 writedlm("PFfitnessobj_1973-1992_Cali_withoutH3(fixSsp)_right.txt", pf_F)
 writedlm("all_Sobj_1973-1992_Cali_withoutH3(fixSsp)_right.txt",All_S)
 writedlm("all_Fobj_1973-1992_Cali_withoutH3(fixSsp)_right.txt",All_F)
