import jsonArray
import time
import json
#from redis.sentinel import Sentinel
import connect
import sys
import numpy
if __name__ == '__main__':
    # ======Try to connect NEON server
  
    #sentinel=Sentinel([('10.1.53.240', 9001),('10.1.53.240', 9011),('10.1.53.240', 9021)], socket_timeout=0.1)
    #sentinel=Sentinel([('10.1.33.141', 9001),('10.1.33.141', 9011),('10.1.33.141', 9021)], socket_timeout=0.1)
    #r=sentinel.master_for('neonmaster', socket_timeout=20, password='sanlie;123')

    ip_port =  [('10.1.33.141', 9001), ('10.1.33.141',9001)]
    #ip_port = [('10.1.53.240', 9001), ('10.1.53.240',9011), ('10.1.53.240',9021)]
    passwd = 'sanlie;123'

    r= connect.connectNeon(ip_port, passwd, 10)
    r_read = r.getRedisRead()

    name=sys.argv[1]
   
    
    # test xy_image data 
    '''
    path1='/GPPD/workspace/MantidData/'+name+'/xy_image/x' 
    path2='/GPPD/workspace/MantidData/'+name+'/xy_image/y' 
    path='/GPPD/workspace/MantidData/'+name+'/xy_image/value' 
    x=r_read.get(path1)
    y=r_read.get(path2)
    v=r_read.get(path)
    x=jsonArray.jsonDecoder(x)
    y=jsonArray.jsonDecoder(y)
    v=jsonArray.jsonDecoder(v)
    print len(x), len(y), numpy.array(v).shape
    '''
    
    # test 1D data
    path1='/GPPD/workspace/MantidData/'+name+'/d'
    #path1='/GPPD/workspace/MantidData/'+name+'/wave'
    path='/GPPD/workspace/MantidData/'+name+'/intensity'
    #path1='/GPPD/workspace/MantidData/'+name+'/tof'
    #path='/GPPD/workspace/MantidData/'+name+'/counts'
    x=r_read.get(path1)
    v=r_read.get(path)
    x=jsonArray.jsonDecoder(x)
    v=jsonArray.jsonDecoder(v)
    print (v)
    print sum(v)
    #print x
    #v1=[]
    #print (v)
    #for ix in v:
    #    v1.append(ix)
    #for i in range(len(v1)):
    #    if not numpy.isnan(v1[i]) and numpy.isfinite(v1[i]):
    #        pass
    #    else:
    #        v1[i]=0.0
    #print v1
    #print path1, path
    print len(x), len(v)
