#general imports
import pygslib    
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import norm
import numpy as np

#make the plots inline
%matplotlib inline
#%matplotlib notebook

#get the data in gslib format into a pandas Dataframe
data= pd.DataFrame({'Z':[2.582,3.087,3.377,3.974,4.321,5.398,8.791,12.037,12.586,16.626]})
data['Declustering Weight'] = 1.0
data

"""
Explanation: Interactive gaussian anamorphosis modeling with hermite polynomials
Adrian Martinez Vargas
Adrian.Martinez@csaglobal.com
PhD in Geological Sciences. Senior Consultant.
CSA Global,
Toronto, Canada.
End of explanation
"""


# Fit anamorphosis by changing, zmax, zmin, and extrapolation function
PCI, H, raw, zana, gauss, z, P, raw_var, PCI_var, fig1 = pygslib.nonlinear.anamor(
                 z = data['Z'], 
                 w = data['Declustering Weight'], 
                 zmin = data['Z'].min()-0.1, 
                 zmax = data['Z'].max()+1,
                 zpmin = None,
                 zpmax = data['Z'].max()+1.5,
                 ymin=-2.9, ymax=2.9,
                 ndisc = 5000,
                 ltail=1, utail=4, ltpar=1, utpar=1.8, K=30)

PCI

"""
Explanation: Interactive anamorphosis modeling
End of explanation
"""


ZV, PV, fig2 = pygslib.nonlinear.anamor_blk( PCI, H, r = 0.6, gauss = gauss, Z = z,
                  ltail=1, utail=1, ltpar=1, utpar=1,
                  raw=raw, zana=zana)

# the pair ZV, PV define the CDF in block support
# let's plot the CDFs
plt.plot (raw,P, '--k', label = 'exp point' ) 
plt.plot (z,P, '-g', label = 'ana point(fixed)' )  #point support (from gaussian anamorphosis)
plt.plot (ZV, PV, '-m',  label = 'ana block(fixed)')  #block support 
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

"""
Explanation: Block support transformation
End of explanation
"""


cutoff = np.arange(0,10, 0.1)
tt = []
gg = []
label = []

# calculate GTC from gaussian in block support 
t,ga,gb = pygslib.nonlinear.gtcurve (cutoff = cutoff, z=ZV, p=PV, varred = 1, ivtyp = 0, zmin = 0, zmax = None,
             ltail = 1, ltpar = 1, middle = 1, mpar = 1, utail = 1, utpar = 1,maxdis = 1000)
tt.append(t)
gg.append(ga)
label.append('DGM with block support')

# calculate GTC using undirect lognormal 
t,ga,gb = pygslib.nonlinear.gtcurve (cutoff = cutoff, z=z, p=P, varred = 0.4, ivtyp = 2, zmin = 0, zmax = None,
             ltail = 1, ltpar = 1, middle = 1, mpar = 1, utail = 1, utpar = 1,maxdis = 1000)
tt.append(t)
gg.append(ga)
label.append('Indirect Lognormal Correction')

# calculate GTC using affine 
t,ga,gb = pygslib.nonlinear.gtcurve (cutoff = cutoff, z=z, p=P, varred = 0.4, ivtyp = 1, zmin = 0, zmax = None,
             ltail = 1, ltpar = 1, middle = 1, mpar = 1, utail = 1, utpar = 1,maxdis = 1000)
tt.append(t)
gg.append(ga)
label.append('Affine Correction')

# calculate GTC in point support  
t,ga,gb = pygslib.nonlinear.gtcurve (cutoff = cutoff, z=z, p=P, varred = 1, ivtyp = 2, zmin = 0, zmax = None,
             ltail = 1, ltpar = 1, middle = 1, mpar = 1, utail = 1, utpar = 1,maxdis = 1000)
tt.append(t)
gg.append(ga)
label.append('Point (anamorphosis without support effect)')

fig = pygslib.nonlinear.plotgt(cutoff = cutoff, t = tt, g = gg, label = label)

"""
Explanation: Grade Tonnage curves
End of explanation
"""


PCI, H, raw, zana, gauss, raw_var, PCI_var, ax2 = pygslib.nonlinear.anamor_raw(
                         z = data['Z'], 
                         w = data['Declustering Weight'], 
                         K=30)

PCI

print (zana)

"""
Explanation: Anamorphosis modeling from raw Z,Y pairs
End of explanation
"""


