"""
Utilities Functions.

Copyright (c) 2014 NavPy Developers. All rights reserved.
Use of this source code is governed by a BSD-style license that can be found in
LICENSE.txt
"""

import numpy as _np

def input_check_Nx1(x):
    """
    Check x to be of dimension Nx1 and reshape it as a 1-D array
    
    Adhika Lie
    """
    x = _np.atleast_1d(x)
    theSize = _np.shape(x)

    if(len(theSize)>1):
        #1. Input must be of size N x 1
        if ((theSize[0]!=1) & (theSize[1]!=1)):
            raise ValueError('Not an N x 1 array')
        #2. Make it into a 1-D array
        x = x.reshape(_np.size(x))
    elif (theSize[0]==1):
        x = x[0]
    
    return x,_np.size(x)

def input_check_Nx3(x):
    """
    Check x to be of dimension Nx3
    
    Adhika Lie
    """
    x = _np.atleast_2d(x)
    theSize = _np.shape(x)
    
    if(len(theSize)>1):
        #1. Input must be of size N x 3
        if ((theSize[0]!=3) & (theSize[1]!=3)):
            raise ValueError('Not a N x 3 array')
        #2. Make it into a Nx3 array
        if (theSize[1]!=3):
            x = x.T
        N = x.shape[0]
        #3. If N == 1, make it into a 1-D array
        if (x.shape[0]==1):
            x = x.reshape(x.shape[1])

    return x,N

def loadtxt2dic(filename):
    """
    Loads text file of key:value pairs into a dictionary.  
    Usage notes:
    -Lines begining with '#' are treated as comments and skipped.  
    -Blank lines are also skipped
    -Keys and values should be separated by '=' or ':', extra spaces are fine.
    -A matrix/scalar are stored floats ONLY if the text has a decimal
    
    Hamid M. (original)
    Adhika Lie 
    """
    fid = open(filename,'r')
    param = {}
    for line in fid:
        # Remove Extra Spaces
        line = line.strip()
        
        # Skip lines beginning with # or blank
        # Note: Python treats '' as False
        if(line.startswith('#') or line.startswith('\n') or (not line)):
            continue
        
        # Split item
        item = line.split('#',1)[0].strip() # May have comment after the line
        item = item.replace(':',' ').replace('=',' ').split(None,1)
        
        if(len(item)==0):
            continue
            
        try:
            param[item[0]] = eval(item[1])
            
            if( type(eval(item[1]))==list ):
                param[item[0]] = _np.array(eval(item[1]))
        
        except NameError:
            param[item[0]] = item[1]
    
    fid.close()
    return param

def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
    """
    Prompt user for for 'yes' or 'no' response
    Taken from Python Documentation with small modifications
    http://docs.python.org/tutorial/controlflow.html
    
    Example:
    >>> ask_ok('Do you really want to quit?')
    
    Hamid M. May 2012
    """
    while True:
    
        ok = raw_input(prompt).lower()
        
        if ok in ('y', 'ye', 'yes','1'):
            return True
        if ok in ('n', 'no', 'nop', 'nope','0'):
            return False
        retries = retries - 1
        if retries < 0:
            raise IOError('refusenik user')
        print complaint

def status_update(i,drl,message,bar_length=20):
    """
    To create progress bar + short error message
    
    Parameters
    ----------
    i is the counter of where you are in the "count"
    drl is the total data record length
    message is your message line (keep it short!)
    
    Adhika Lie
    """
    percent = float(i) / drl
    hashes = '#' * int(round(percent * bar_length))
    spaces = ' ' * (bar_length - len(hashes))
    
    mmsg = message
    if(len(mmsg)>0):
        if(mmsg[-1]!='\n'):
            mmsg = mmsg + '\n'
    
    sys.stdout.write("\r[%s] %3d%% :: %s" % (hashes + spaces, int(round(percent * 100)),mmsg))
    sys.stdout.flush()

@@ -1697,16 +1697,40 @@
 am = %7B%7D%0A
+    prev_line = ''%0A    %0A
     for 
@@ -1786,16 +1786,28 @@
   line =
+ prev_line +
  line.st
@@ -1812,32 +1812,42 @@
 strip()%0A        
+print line
 %0A        # Skip 
@@ -2015,32 +2015,244 @@
 ntinue%0A        %0A
+        # If line ends with a comma, it continues to the next line.%0A        if(line.endswith(',')):%0A            prev_line = line.strip()%0A            continue%0A        else:%0A            prev_line = ''%0A            %0A
         # Split 

Add multiline array features.