zt.append(z)

xt = np.asarray(xt)
yt = np.asarray(yt)
zt = np.asarray(zt)

#x0 = range(3000,4000,50)
#y0 = range(2000,3000,50)
#z0 = range(0,30,1)
xp = len(xt)
yp = len(yt)
zp = len(zt)

pt = xp
timet, par0 = advect_functions.read_particles_csv(filename0,pt,tt)
par0p = lagrangian_stats.periodicCoords(par0,2000,2000)
time0 = (timet)*360 + 48*3600 - 360
time0 = np.asarray(range(len(timet)))*360 + 48*3600 - 360

par = np.reshape(par0p,(pt,3,tt))

# FSLE
di = 5
time = time0 - time0[0]

rad = [5,10,20]

depths = [0, -10, -20] # z0

def dist(x0,x1,y0,y1,z0,z1):
 dr = np.sqrt((x0-x1)**2 + (y0-y1)**2 + (z0-z1)**2)
tt = min(len(time2D),len(time3D))

par3D = par3D[:,:,:tt]
par2D = par2D[:,:,:tt]

time = time2D[:tt]

# horizontal
depths = [1, 5, 11, 17, 26]
depths = [1, 5, 17]
#depths = [5] #, 17, 1]

# periodicity

par2DP = lagrangian_stats.periodicCoords(par2D,4000,10000)
par3DP = lagrangian_stats.periodicCoords(par3D,4000,10000)

for z in depths: 
 print 'depth', z
 par2Dz = np.reshape(par2DP,(20,20,30,3,tt))
 par3Dz = np.reshape(par3DP,(20,20,30,3,tt))
 #
 par2Dzr = par2Dz[:,:,z,:,:]
 par3Dzr = par3Dz[:,:,z,:,:]
 #
 par2Dz = np.reshape(par2Dzr,(400,3,tt))
 par3Dz = np.reshape(par3Dzr,(400,3,tt))
 #

 for t in range(0,tt,5):
filename0 = 'traj_m_25_2_512_0_48.csv'
tt = 48 # IC + 24-48 included
#x0 = range(0,2000,50)
#y0 = range(0,2000,50)
#z0 = range(0,50,2)
x0 = range(3000,4000,50)
y0 = range(2000,3000,50)
z0 = range(0,30,1)
xp = len(x0)
yp = len(y0)
zp = len(z0)

pt = xp*yp*zp
timet, par0 = advect_functions.read_particles_csv(filename0,xp,yp,zp,tt)

par0 = lagrangian_stats.periodicCoords(par0,10000,4000)

time0 = (timet)*1200 + 48*3600 - 1200

# read online
print 'reading online'
exp = 'm_25_2_512'

try: os.stat('./plot/'+exp)
except OSError: os.mkdir('./plot/'+exp)

filename0 = './mli_tracer.detectors'
filename0 = '/tamay2/mensa/fluidity/'+exp+'/'+filename0

time1, parT = lagrangian_stats.read_particles(filename0)
y0 = range(0,5010,10)
#y0 = range(0,4010,10)
z0 = [0,5,10,15]

#BW
#x0 = range(0,9010,10)
#y0 = range(0,3010,10)
#z0 = [0,5,10,15]

xp = len(x0)
yp = len(y0)
zp = len(z0)
pt = xp*yp*zp

time2D, par2D = advect_functions.read_particles_csv(filename2D,pt,tt)
par2D = lagrangian_stats.periodicCoords(par2D,8000,8000)

#
time2D = (time2D)*1440
#    
time = time2D[:-1]

depths = [5, 10, 15] 
depthid = [1, 2, 3] 

nl = len(depths)

RD_2D = np.zeros((tt,nl))
CD_2D = np.zeros((tt,nl))
ED_2D = np.zeros((tt,nl))
#x0 = range(3000,4010,10)
#y0 = range(2000,3010,10)
#z0 = range(1,20,4)

#tt = 230 # IC + 24-48 included
#x0 = range(3000,4010,10)
#y0 = range(2000,3010,10)
#z0 = range(1,20,4)

xp = len(x0)
yp = len(y0)
zp = len(z0)
pt = xp*yp*zp

time2D, par2D = advect_functions.read_particles_csv(filename2D,pt,tt)
par2D = lagrangian_stats.periodicCoords(par2D,2000,2000)
time3D, par3D = advect_functions.read_particles_csv(filename3D,pt,tt)
par3D = lagrangian_stats.periodicCoords(par3D,2000,2000)
#
time2D = (time2D)*360 - 360
time3D = (time3D)*360 - 360

time0 = time2D

# horizontal
depths = [0,1,2] #[1, 5, 11, 17, 26]
#depths = [5]
#depths = [1] #, 17, 1]


K2xt=[]
tt_B = 600 # IC + 24-48 included

Xlist_BW = np.linspace(0,10000,801)
Ylist_BW = np.linspace(0,4000,321)
Xlist_B = np.linspace(0,8000,641)
Ylist_B = np.linspace(0,8000,641)
dl = [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1]
Zlist = 1.*np.cumsum(dl)

xp = 101
yp = 101
zp = 4
pt = xp*yp*zp
          
time2D_B, par2D_B = advect_functions.read_particles_csv(filename2D_B,pt,tt_B)
par2D_B = lagrangian_stats.periodicCoords(par2D_B,8000,8000)
time3D_B, par3D_B = advect_functions.read_particles_csv(filename3D_B,pt,tt_B)
par3D_B = lagrangian_stats.periodicCoords(par3D_B,8000,8000)

time2D_B = (time2D_B)*1440
time3D_B = (time3D_B)*1440 

time2D_BW, par2D_BW = advect_functions.read_particles_csv(filename2D_BW,pt,tt_BW)
par2D_BW = lagrangian_stats.periodicCoords(par2D_BW,10000,4000)
time3D_BW, par3D_BW = advect_functions.read_particles_csv(filename3D_BW,pt,tt_BW)
par3D_BW = lagrangian_stats.periodicCoords(par3D_BW,10000,4000)

time2D_BW = (time2D_BW)*1440
time3D_BW = (time3D_BW)*1440
   
time = time2D_BW[:-1]