import src.model_Production as modPro
import src.surface_Brightness_Profiles as SBPro
import numpy as np

#Single Run - Derivative
print 'Running'

imageParams = modPro.default_ModelParameter_Dictionary()
imageParams['SNR'] = 35.
imageParams['e1'] = 0.3
imageParams['e2'] = 0.
imageParams['size'] = 0.84853
imageParams['flux'] = 4.524
imageParams['stamp_size'] = [10, 10]
imageParams['centroid'] = (np.array(imageParams['stamp_size']) + 1) / 2.

###Get image using GALSIM default models
#image, imageParams = modPro.get_Pixelised_Model(imageParams, noiseType = None, outputImage = True)
#image, imageParams = modPro.get_Pixelised_Model(imageParams, noiseType = None, outputImage = True, sbProfileFunc = modPro.gaussian_SBProfile)

##A Halls version
image = np.genfromtxt('/home/cajd/Downloads/fid_image.dat')
dimage = np.genfromtxt('/home/cajd/Downloads/dfid_image.dat')
ddimage = np.genfromtxt('/home/cajd/Downloads/ddfid_image.dat')
image = image.T
dimage = dimage.T
ddimage = ddimage.T
print 'Got Original'

print 'Halls:', np.power(image, 2.).sum()
import src.model_Production as modPro
import src.measure_Bias as mBias
import src.image_measurement_ML as ML
import numpy as np

## Bias Measurement
S0 = 1.2; derLabel = 'T'
imageParams = modPro.default_ModelParameter_Dictionary()
imageParams['SNR'] = 50.
imageParams['size'] = 1.2
imageParams[derLabel] = S0
imageParams['stamp_size'] = np.array([50,50])
imageParams['centroid'] = (imageParams['stamp_size']+1)/2.

###Get image
image, imageParams = modPro.get_Pixelised_Model(imageParams, noiseType = 'G')

modelParams = imageParams.copy()

print 'modelParams test:', modelParams['stamp_size'], modelParams['noise']
print 'Estimated variance:', ML.estimate_Noise(image, maskCentroid = modelParams['centroid'])
raw_input('Check')

##Produce analytic bias
print 'Getting analytic bias:'
anbias = mBias.analytic_GaussianLikelihood_Bias(S0, derLabel, modelParams, diffType = 'ana')
print '\n ****** Analytic Bias is:', anbias

##Produce analytic bias
print 'Getting analytic bias:'
numanbias = mBias.analytic_GaussianLikelihood_Bias(S0, derLabel, modelParams, diffType = 'num')
fitParamsLabels = fittedParameters.keys(); fitParamsValues = fittedParameters.values()

## preSearchMethod defines whether a grid-based method is used to define an initial guess. Will give a lot of slow-down for large parameter spaces, but likely to reduce the effect of local mimina or dependancies on initial guesses
preSearchMethod = 'grid'
## bruteRange must be a tuple of 2-element lists (or three element slice), even in the 1D case
#bruteRange = [(-0.9, 0.9)]
bruteRange = [(0.21, 0.39), (0.21, 0.39)]

## If >=1, the ML Estiamtor routine will correct to that order (only coded to first order as of 31 Aug 2015)
biasCorrect = 1

##Initial Galaxy Set up
imageShape = (15., 15.) #size = 0.84853
imageParams = modPro.default_ModelParameter_Dictionary(SB = dict(size = 1.2, e1 = 0.0, e2 = 0.0, magnification = 1., shear = [0., 0.], flux = 4.524, modelType = 'gaussian'),\
                                                       centroid = (np.array(imageShape)+1)/2, noise = 10., SNR = 50., stamp_size = imageShape, pixel_scale = 1.,\
                                                       PSF = dict(PSF_Type = 0, PSF_size = 1., PSF_Gauss_e1 = 0.1, PSF_Gauss_e2 = 0.0)
                                                       )


## Model Lookup Defintions - Overridden in the number of fitted parameters is greater than one
useLookup = False
## 1D Ellipticity lookup
'''
lookupRange = [-0.99, 0.99]
lookupWidth = [0.001]
'''
##2D Ellipticity Lookup
'''
lookupRange = [[-0.99, 0.99],[-0.99, 0.99]]
lookupWidth = [0.01,0.01]
'''