#from simParam import *
import math, re
import os
#from units import *

## Examples:
##http://matplotlib.github.com/api/pyplot_api.html#matplotlib.pyplot.plot
## HINT: startplot == False, can be used to generate multiple plots
plot = []

show_plot = True
var='vel'
var='press'

files = ['*_'+var+'_*.res' ]
resultFolder  = 'prodRe25_multilevel'
stringToFind  = 'diffusive'
stringToFind2 = 'ref1'
stringToFind3 = ''
folders = []
minlevel = 0
maxlevel = 5
# Set up the list of simulation folders in the results directory
for dirname, dirnames, filenames in os.walk(resultFolder):
  isSimDir = False
  # if I find tracking as subdirectory, mark as a simulation directory
  if 'tracking' in dirnames:
    if ( stringToFind in dirname ) and ( stringToFind2 in dirname ) and ( stringToFind3 in dirname ):
        print 'dirnames', dirname
        isSimDir = True
  # if it was marked as a simulation directory, append it to the folder list
  if isSimDir:
    includeInAnalysis = False
    print 'dirname', dirname
    print 'regex  ', re.search(r"(?<=level)[0-9]*?(?=ref)", dirname)
    level = re.search(r"(?<=level)[0-9]*?(?=ref)", dirname).group(0)
    if int(level) >= minlevel and int(level) <= maxlevel:
      print 'level is', level
      includeInAnalysis = True
    if includeInAnalysis: 
      folders.append(dirname)

print 'Found simulation folders:'
print folders

# directory loop
for iFolder in range(len(folders)):
  if iFolder == 0:
    startPlt = True
  else:
    startPlt = False
  if iFolder == len(folders)-1:
    endPlt = True
  else:
    endPlt = False

  level = re.search(r"(?<=level)[0-9]*?(?=ref)", folders[iFolder]).group(0)
  intp  = re.search(r"(?<=intp)[a-z]*?(?=rel)", folders[iFolder]).group(0)

  print 'acting on folder', folders[iFolder], files[0]
  ## xy plot 
  plot.append(dict(kind = 'xy',
              data=[folders[iFolder]+'/tracking/'+files[0]], format='eps',
              figname='test', ls='-',
              row=[1,-1],col=[1,2], startplot = startPlt, endplot = endPlt,
              label  = 'level '+level+' intp '+intp ))


