###modelling free parameters are:
# space craft distance
# subspacecraft long,lat,az
# subsolar long, lat, az
# y,z image offsets in km
# offsetVel in km/s
# offsetVelAngle in degrees
###Chosen parameters are:
# polyOrder for the velocity modelling

if loadBestPoint:
    (bestPoint,goodSamps)=getFit('/Users/fraserw/data/VIMS/covims_0004/procdata/%s.fit_pickle'%(imageName))
    (ll,l)=goodSamps.shape
    #w=num.where(goodSamps[:,13]>bestPoint[13]-2)
    #for i in range(len(w[0])):
    #    print goodSamps[w[0][i]]
    #sys.exit()
    [long_o_not,lat_o_not,az_o_not,long_s,lat_s,az_s,distancenot,offXV,offYV,offXI,offYI,offsetVel,offsetVelAngle,chi]=bestPoint
    offsetsVis=num.array([offXV,offYV])
    offsetsIR =num.array([offXI,offYI])
    

(imageVis,poly3d,colours,rot_vert,vertsInPixelVis,chi)=shapeGen_VIMS(vertices,vertIndices,
                                                    long_o_not,lat_o_not,az_o_not,
                                                    long_s,lat_s,az_s,
                                                    distancenot,
                                                    offsetsVis, offsetVel,offsetVelAngle,
    sampler.reset()

    pos, prob, state = sampler.run_mcmc(pos, nStep, rstate0=state)

    samps=sampler.chain
    probs=sampler.lnprobability

    with open('/data/VIMS/covims_0004/procdata/%s.fit_pickle'%(imageName),'w+') as outHan:
        pick.dump([samps,probs],outHan)
    
    print sampler.chain
    print
    print sampler.lnprobability

    (bestPoint,goodSamps)=getFit('/data/VIMS/covims_0004/procdata/%s.fit_pickle'%(imageName))

    print bestPoint
    if exitAfterFit: sys.exit()

if doFitsNoVel:
    x,y=0.,0.
    
    nDim=11
    nWalkers=nDim*4
    nBurn=30
    nStep=30

    steps=0
    offWidth=20.
    angWidth=10.