import sys
import os
import re

# name of control file (hardwired, but should work fine
filename = "system/controlDict"


# open controlDict
with open(filename) as f:
    content = f.readlines()


# remove brackets from all lines
content = [element.translate(None,"()\n") for element in content]
# remove empty lines
# print content


# probe counter
nprobes = 0
# array with probe locations
probelocations = [] 

# flag indicates if script has stepped into the probe location number section of file
numblock = False

# look for specific probe list
enable = False
if enable:
    # flag indicates if script has found the specific probe list
    listname = False
    listnamestring = "probes_polyline"
    regex = r"\b" + re.escape(listnamestring) + r"\b"
    print "Search for " + regex
else:
    listname = True


# step through each line of the file and look for probeLocation keyword marking the start of the probe location block

for ln in content:
    if enable:
        if re.search(regex, ln):
            #print ln
            listname = True
            continue
        if "}" in ln:
            listname = False
            continue

    # look for keyword
    if "probeLocations" in ln and listname:
        #print "hit"
        numblock = True
        continue
    # read numbers after keyword found
    if numblock == True:
        try:
            # remove comments 
            ln = ln.split('//')[0]
            # read 3 number as float
            loc = map(float, ln.split())
            if loc != []:
                probelocations.append(loc)
                #print loc
                nprobes += 1
        # look for other probe blocks
        except:
            numblock = False

#del probelocations[0]
#print nprobes
#print probelocations

#print probelocations[2]
#content.translate(None,"()")
#lines = f.readlines()

#print lines

f.close()





rownum = 0
Source = GetActiveSource()
for probe in probelocations:
#    print "rownum------------------>", rownum + 1
    #print probe[0], probe[1], probe[2]
#    Sphere1 = Sphere()
    #PointSource1 = PointSource()
    ProbeLocation1 = ProbeLocation(Source)
    #RenderView1 = GetRenderView()
    #DataRepresentation2 = Show()

    #PointSource1.Center = probe
    ProbeLocation1.ProbeType.Center = probe

    ProbeRepresentation = GetRepresentation()
    ProbeRepresentation.PointSize = 5
    ProbeRepresentation.ColorArrayName = ['POINTS', '']
    #Glyph2 = Glyph( GlyphType="Sphere" )
    mytext = a3DText()
    mytext.Text = str(rownum+1)
    
    trafo = Transform(Input=mytext)
    trafo.Transform.Scale = [0.01,0.01,0.01]
    trafo.Transform.Translate = probe
    Show(trafo)

    #Glyph2.GlyphType.Radius = 0.05

    #DataRepresentation3 = Show()
    rownum = rownum + 1
#Render()

