def lireTrajectoires(nomFichier, segmentationTab):
    f=open(nomFichier[-36:], 'w'); toCheck={}
#    (X, Y, upX, upY, dX, dY) = coord(nomTxt)
#    frameLabels = X.keys()
    try:
        fichierX =  open(nomFichier, "r")
    except IOError:
        print nomFichier, " n'existe pas"
        return None
    listK = []
    ensembleTraj = ensTraj()
    traj = trajectoire(0,0,0,0,0)
    k=0
    continuer = True
    mesLignes = fichierX.readlines()
    mesLignes.reverse()

    while continuer:
        ligneCourante = mesLignes.pop()
        if "<Type>" in ligneCourante:
            if not ensembleTraj.lstTraj==[]:
                traj.ajoutPoint(0, 0, frame+1, -2)

            ensembleTraj.ajouter(traj)
            k = int(ligneCourante[14:-8])
            listK.append(k)
            nouveau = True

        elif "<MarkerX>" in ligneCourante:
            xi = int(ligneCourante[19:-11])
            ligne2 = mesLignes.pop()
            yi = int(ligne2[19:-11])
            ligne3 = mesLignes.pop()
            frame = int(ligne3[19:-11])-1
            #print xi, yi, frame
            
            #ici, plutot charger le fichier hdf5 avec la segmentation et regarder a quel label la coordonnee correspond dans le fichier hdf5
            #donc on va utiliser le tableau arraySegmentation qui contient toute la segmentation de l'image

            segmentationF = segmentationTab[0][frame][0][:][:]
            l=segmentationF[yi][xi]
            if int(l)==0:
                print "ATTENTION CELL IDENTIFICATION PROBLEM ON FRAME ", frame, xi, yi
                if frame not in toCheck:
                    toCheck.update({frame:[]})
                toCheck[frame].append((xi, yi))
                #raise
#            if frame==34:
#                pdb.set_trace()
#            for f in frameLabels:
#                if int(f[0])==int(frame):
#                    upxi =int(upX[f])
#                    dxi=int(dX[f])
#                    upyi = int(upY[f])
#                    dyi=int(dY[f])
#                    if int(dxi)>=int(xi) and int(xi)>=int(upxi) and int(dyi)>=int(yi) and int(yi)>=int(upyi):
#                        l2 = f[1]
#                        #if frame == 91 or frame==92: print "TROUVE "+ str(dxi), upxi, xi, dyi, upyi, yi, l, frame
                        
#            if (l!=l2): print "les deux labels ne sont pas identiques attention"
            
            if nouveau:
              #  print "ajout de la trajectoire "+str(k)
                if int(frame)>0:
                    traj = trajectoire(k, 0, 0, frame-1, -1)
                    traj.ajoutPoint(xi, yi, frame, l)
                else: 
                    traj = trajectoire(k, xi, yi, frame, l)
               # ensembleTraj.ajouter(traj)

            else:
                #traj= ensembleTraj.trouver(k).lstTraj[0]
                traj.ajoutPoint(xi, yi, frame, l)

            nouveau = False

        elif "</Marker_Data>" in ligneCourante:
            traj.ajoutPoint(0, 0, frame+1, -2)
            ensembleTraj.ajouter(traj)
            continuer = False

 #   for traj in ensembleTraj.lstTraj: print traj.lstPoints
    f.write(fHacktrack2.initXml())
    for index in toCheck:
        zz=1
        for chpt in toCheck[index]:
            result = "\n      <Marker_Type>\n        <Type>{0}</Type>".format(zz)
            result+="\n        <Marker>\n          <MarkerX>{1}</MarkerX>\n          <MarkerY>{2}</MarkerY>\n          <MarkerZ>{0}</MarkerZ>\n        </Marker>".format(index+1, chpt[0],chpt[1])
        
            result+="\n      </Marker_Type>"
            f.write(result)
            zz+=1
    f.write (fHacktrack2.finirXml())
            
    fichierX.close(); f.close()
    return ensembleTraj
def lireTrajectoires(nomFichier, nomTxt):
    (X, Y, upX, upY, dX, dY) = coord(nomTxt)
    frameLabels = X.keys()

    fichierX =  open(nomFichier, "r")
    listK = []
    ensembleTraj = ensTraj()
    traj = trajectoire(0,0,0,0,0)
    k=0
    continuer = True
    mesLignes = fichierX.readlines()
    mesLignes.reverse()

    while continuer:
        ligneCourante = mesLignes.pop()
        #print ligneCourante
        if "<Type>" in ligneCourante:
            if not ensembleTraj.lstTraj==[]:
                traj.ajoutPoint(0, 0, frame+1, -2)

            ensembleTraj.ajouter(traj)
           # print ligneCourante
            k = int(ligneCourante[14:-8])
            #print k
            listK.append(k)
            nouveau = True

        elif "<MarkerX>" in ligneCourante:
            xi = int(ligneCourante[19:-11])
            ligne2 = mesLignes.pop()
            yi = int(ligne2[19:-11])
            ligne3 = mesLignes.pop()
            frame = int(ligne3[19:-11])-1
            #print xi, yi, frame

            for f in frameLabels:
                if int(f[0])==int(frame):
                    upxi =int(upX[f])
                    dxi=int(dX[f])
                    upyi = int(upY[f])
                    dyi=int(dY[f])
                    #print "ici", upxi, upyi, dxi, dyi
                    if int(dxi)>=int(xi) and int(xi)>=int(upxi) and int(dyi)>=int(yi) and int(yi)>=int(upyi):
                        l = f[1]
                        #if frame == 91 or frame==92: print "TROUVE "+ str(dxi), upxi, xi, dyi, upyi, yi, l, frame

            if nouveau:
              #  print "ajout de la trajectoire "+str(k)
                if int(frame)>0:
                    traj = trajectoire(k, 0, 0, frame-1, -1)
                    traj.ajoutPoint(xi, yi, frame, l)
                else: 
                    traj = trajectoire(k, xi, yi, frame, l)
               # ensembleTraj.ajouter(traj)

            else:
                #traj= ensembleTraj.trouver(k).lstTraj[0]
                traj.ajoutPoint(xi, yi, frame, l)

            nouveau = False

        elif "</Marker_Data>" in ligneCourante:
            traj.ajoutPoint(0, 0, frame+1, -2)
            ensembleTraj.ajouter(traj)
            continuer = False

 #   for traj in ensembleTraj.lstTraj: print traj.lstPoints

    fichierX.close()
    return ensembleTraj