rescale_index_psf3=5
params_index_psf4=1
rescale_index_psf4=2

if eventclass==2:
    psf_file_name = 'psf_P8R2_SOURCE_V6_PSF.fits'
    theta_norm_psf1=[0.0000000,9.7381019e-06,0.0024811595,0.022328802,0.080147663,0.17148392,0.30634315,0.41720551]
    theta_norm_psf2=[0.0000000,0.00013001938,0.010239333,0.048691643,0.10790632,0.18585539,0.29140913,0.35576811]
    theta_norm_psf3=[0.0000000,0.00074299273,0.018672204,0.062317201,0.12894928,0.20150553,0.28339386,0.30441893]
    theta_norm_psf4=[4.8923139e-07,0.011167475,0.092594658,0.15382001,0.16862869,0.17309118,0.19837774,0.20231968]
elif eventclass==5:
    psf_file_name = 'psf_P8R2_ULTRACLEANVETO_V6_PSF.fits'
    theta_norm_psf1=[0.0000000,9.5028121e-07,0.00094418357,0.015514370,0.069725775,0.16437751,0.30868705,0.44075016]
    theta_norm_psf2=[0.0000000,1.6070284e-05,0.0048551576,0.035358049,0.091767466,0.17568974,0.29916159,0.39315185]
    theta_norm_psf3=[0.0000000,0.00015569366,0.010164870,0.048955837,0.11750811,0.19840060,0.29488095,0.32993394]
    theta_norm_psf4=[0.0000000,0.0036816313,0.062240006,0.14027030,0.17077023,0.18329804,0.21722594,0.22251374]

f = fits.open('/zfs/bsafdi/data/psf_data/' + psf_file_name)

kparam=PSFKC.PSF_king(f, theta_norm=theta_norm_psf1, rescale_index=rescale_index_psf1, params_index=params_index_psf1)

fcore=kparam.return_king_params(energyval,'fcore')
score=kparam.return_king_params(energyval,'score')
gcore=kparam.return_king_params(energyval,'gcore')
stail=kparam.return_king_params(energyval,'stail')
gtail=kparam.return_king_params(energyval,'gtail')
SpE=kparam.rescale_factor(energyval)

pk = pki.psf_king(fcore,score,gcore,stail,gtail,SpE,save_tag='-king_example_1pdf_trunc=0.001',n_ps=50000,f_trunc=0.001)
pk.make_PSF()
import numpy as np
from astropy.io import fits
import PSF_king_class as PSFKC
import PSF_class as PSFC
import psf_king as pki

energyvalarray = np.array([0.336605534472,0.423761259986,0.533483821671,0.67161633,0.84551489,1.0644402,1.3400508,1.6870239,2.1238373,2.6737528,3.3660554,4.2376127,5.3348382,6.7161634,8.4551486,10.644402,13.400508,16.870240,21.238374,26.737528,33.660553,42.376126,53.348382,67.161633,84.551489,106.44402,134.00508,168.70240,212.38374,267.37528])

eventclass = 2 # 2 for source, 5 for ultracleanveto

# Load required inputs
params_index_psf1=10
rescale_index_psf1=11
psf_file_name = 'psf_P8R2_ULTRACLEANVETO_V6_PSF.fits'
theta_norm_psf1=[0.0000000,9.5028121e-07,0.00094418357,0.015514370,0.069725775,0.16437751,0.30868705,0.44075016]

f = fits.open('/zfs/bsafdi/data/psf_data/' + psf_file_name)

kparam=PSFKC.PSF_king(f, theta_norm=theta_norm_psf1, rescale_index=rescale_index_psf1, params_index=params_index_psf1)

for i in np.arange(8,17):
    fcore=kparam.return_king_params(energyvalarray[i],'fcore')
    score=kparam.return_king_params(energyvalarray[i],'score')
    gcore=kparam.return_king_params(energyvalarray[i],'gcore')
    stail=kparam.return_king_params(energyvalarray[i],'stail')
    gtail=kparam.return_king_params(energyvalarray[i],'gtail')
    SpE=kparam.rescale_factor(energyvalarray[i])
    pk = pki.psf_king(fcore,score,gcore,stail,gtail,SpE,save_tag='-bin'+str(i),n_ps=50000)
    pk.make_PSF()
fcore2=kparam.return_king_params(energyval,'fcore')
score2=kparam.return_king_params(energyval,'score')
gcore2=kparam.return_king_params(energyval,'gcore')
stail2=kparam.return_king_params(energyval,'stail')
gtail2=kparam.return_king_params(energyval,'gtail')
SpE2=kparam.rescale_factor(energyval)

# Load Q3 params
kparam=PSFKC.PSF_king(f, theta_norm=theta_norm_psf3, rescale_index=rescale_index_psf3, params_index=params_index_psf3)

fcore3=kparam.return_king_params(energyval,'fcore')
score3=kparam.return_king_params(energyval,'score')
gcore3=kparam.return_king_params(energyval,'gcore')
stail3=kparam.return_king_params(energyval,'stail')
gtail3=kparam.return_king_params(energyval,'gtail')
SpE3=kparam.rescale_factor(energyval)

# Load Q4 params
kparam=PSFKC.PSF_king(f, theta_norm=theta_norm_psf4, rescale_index=rescale_index_psf4, params_index=params_index_psf4)

fcore4=kparam.return_king_params(energyval,'fcore')
score4=kparam.return_king_params(energyval,'score')
gcore4=kparam.return_king_params(energyval,'gcore')
stail4=kparam.return_king_params(energyval,'stail')
gtail4=kparam.return_king_params(energyval,'gtail')
SpE4=kparam.rescale_factor(energyval)

pk = pki.psf_king([fcore1,fcore2,fcore3,fcore4],[score1,score2,score3,score4],[gcore1,gcore2,gcore3,gcore4],[stail1,stail2,stail3,stail4],[gtail1,gtail2,gtail3,gtail4],[SpE1,SpE2,SpE3,SpE4],save_tag='-king_example_2pdf',n_ps=50000,frac=[0.5,0.5,0.5,0.5])
pk.make_PSF()