import os, sys
import operator
import glob
import numpy as np

# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

def writeLog( parser , args, skip=False ):
  try:
    if( not skip ):
      import time
      td = time.localtime()
      progname = parser.prog
      fstr = '.'+progname.split('.py')[0]
      fstr += '_{}h{}min{}sec_{:02d}{:02d}{}.log'.format(\
        td.tm_hour,td.tm_min,td.tm_sec,td.tm_mday,\
        td.tm_mon,td.tm_year)
  
      fl = open(fstr, 'w')
      logStr = progname
      for arg, value in sorted(vars(args).items()):
        logStr+=" --{}  {} ".format(arg, value)
      fl.write( logStr )
      fl.write( '\n' )
      fl.close()
      logStr = fstr = None
  except:
    pass    

# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

def sortTimes(resultDir="./"):
  dirList = os.listdir(resultDir)         # Obtain the list of directories
  #dirList.sort()                   # Sort the list - this won't work properly for floats (in string format)
  # We need a more robust sorting which uses numerical values
  # This is accomplished by using tuples.
  dirTuple = []
  for dirStr in dirList:
    try:
      dirTuple.append((dirStr, float(dirStr)) ) # (str,float)
    except ValueError:
      pass
  # Sort according to the numerical values: key=operator.itemgetter(1)
  sortedTuple = sorted(dirTuple, key=operator.itemgetter(1))
  sortedList = map(operator.itemgetter(0), sortedTuple) # Extract a list of sorted strings

  return sortedList

# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
# =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*

def selectFromList( L ):
    n = 0
    for entry in L:
        print(" # [{}]: {}".format(n,entry))
        n+=1
    
    #print("\n Enter the selection number(s): \n")
    Ids = []
    e = input(" Selection number(s): ")
    if( e == ''): 
      select = input(" Select All? [1-9]=> Yes, [Empty]=> No: ")