# Path: pyneb/utils/misc.py
# def parseAtom(atom):
#     '''
#     Parses an atom label into the element and spectrum parts
#     '''
# 
#     iso = ''
#     elem = ''
#     spec = ''
#     cont = True
#     firstdigit = True
#     for l in atom:
#         if l.isalpha() and cont:
#             elem += l
#             firstdigit = False
#         elif l.isdigit():
#             if firstdigit:
#                 iso += l
#             else:
#                 spec += l
#                 cont = False
#     return iso+str.capitalize(elem), spec
# 
# def roman_to_int(input_):
#     """
#     Convert a roman numeral to an integer.
#     
#     >>> r = range(1, 4000)
#     >>> nums = [int_to_roman(i) for i in r]
#     >>> ints = [roman_to_int(n) for n in nums]
#     >>> print r == ints
#     1
#     
#     >>> roman_to_int('VVVIV')
#     Traceback (most recent call last):
#      ...
#     ValueError: input is not a valid roman numeral: VVVIV
# 
#     >>> roman_to_int(1)
#     Traceback (most recent call last):
#      ...
#     TypeError: expected string, got <type 'int'>
# 
#     >>> roman_to_int('a')
#     Traceback (most recent call last):
#      ...
#     ValueError: input is not a valid roman numeral: A
# 
#     >>> roman_to_int('IL')
#     Traceback (most recent call last):
#      ...
#     ValueError: input is not a valid roman numeral: IL
# 
#     """   
#     if type(input_) != type(""):
#         #raise TypeError, "expected string, got %s" % type(input_)
#         return None
#     input_ = input_.upper()
#     nums = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
#     ints = [1000, 500, 100, 50, 10, 5, 1]
#     places = []
#     for c in input_:
#         if not c in nums:
#             #raise ValueError, "input is not a valid roman numeral: %s" % input_
#             return None
#     for i in range(len(input_)):
#         c = input_[i]
#         value = ints[nums.index(c)]
#         # If the next place holds a larger number, this value is negative.
#         try:
#             nextvalue = ints[nums.index(input_[i + 1])]
#             if nextvalue > value:
#                 value *= -1
#         except IndexError:
#             # there is no next place.
#             pass
#         places.append(value)
#     sum_ = 0
#     for n in places: sum_ += n
#     # Easiest test for validity...
#     if int_to_roman(sum_) == input_:
#         return sum_
#     else:
#         #raise ValueError, 'input is not a valid roman numeral: %s' % input_
#         return None


import numpy as np
import pyneb as pn
from .misc import parseAtom, roman_to_int
from glob import glob
from copy import deepcopy
    from .manage_atomic_data import getLevelsNIST
        except:
            if verbose:
                print('Unknown Z')
            return 'unknown'    
        
        # Z-I :     
        ZmI = z - int(i_str)
        conf = ('s1', 's2', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6','d7', 'd8')
        cn = np.array([2, 10, 28, 46, 54, 78])
        #cn = np.array([2, 10, 18, 36, 54, 86])
        k = ZmI - cn
        try:
            k_res = np.min(k[k>=0])
            res = conf[k_res]
            if verbose:
                print('Obtained from formula')
        except:
            if verbose:
                print('Unknown conf')
            res = 'unknown'

    
    return res 

def make_gsconf_file(outfile='gsconfs.dat'):
    file_names = sorted(glob('*levels.dat'))
    with open(outfile, 'w') as of:
        for fname in file_names:
            elem, ion, foo = fname.split('_')
            elem = elem.capitalize()

# Path: pyneb/utils/misc.py
# def parseAtom(atom):
#     '''
#     Parses an atom label into the element and spectrum parts
#     '''
# 
#     iso = ''
#     elem = ''
#     spec = ''
#     cont = True
#     firstdigit = True
#     for l in atom:
#         if l.isalpha() and cont:
#             elem += l
#             firstdigit = False
#         elif l.isdigit():
#             if firstdigit:
#                 iso += l
#             else:
#                 spec += l
#                 cont = False
#     return iso+str.capitalize(elem), spec
# 
# def roman_to_int(input_):
#     """
#     Convert a roman numeral to an integer.
#     
#     >>> r = range(1, 4000)
#     >>> nums = [int_to_roman(i) for i in r]
#     >>> ints = [roman_to_int(n) for n in nums]
#     >>> print r == ints
#     1
#     
#     >>> roman_to_int('VVVIV')
#     Traceback (most recent call last):
#      ...
#     ValueError: input is not a valid roman numeral: VVVIV
# 
#     >>> roman_to_int(1)
#     Traceback (most recent call last):
#      ...
#     TypeError: expected string, got <type 'int'>
# 
#     >>> roman_to_int('a')
#     Traceback (most recent call last):
#      ...
#     ValueError: input is not a valid roman numeral: A
# 
#     >>> roman_to_int('IL')
#     Traceback (most recent call last):
#      ...
#     ValueError: input is not a valid roman numeral: IL
# 
#     """   
#     if type(input_) != type(""):
#         #raise TypeError, "expected string, got %s" % type(input_)
#         return None
#     input_ = input_.upper()
#     nums = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
#     ints = [1000, 500, 100, 50, 10, 5, 1]
#     places = []
#     for c in input_:
#         if not c in nums:
#             #raise ValueError, "input is not a valid roman numeral: %s" % input_
#             return None
#     for i in range(len(input_)):
#         c = input_[i]
#         value = ints[nums.index(c)]
#         # If the next place holds a larger number, this value is negative.
#         try:
#             nextvalue = ints[nums.index(input_[i + 1])]
#             if nextvalue > value:
#                 value *= -1
#         except IndexError:
#             # there is no next place.
#             pass
#         places.append(value)
#     sum_ = 0
#     for n in places: sum_ += n
#     # Easiest test for validity...
#     if int_to_roman(sum_) == input_:
#         return sum_
#     else:
#         #raise ValueError, 'input is not a valid roman numeral: %s' % input_
#         return None


# Path: pyneb/utils/physics.py
import numpy as np
import pyneb as pn
from .misc import parseAtom, roman_to_int
from glob import glob
from copy import deepcopy
    from .manage_atomic_data import getLevelsNIST

        except:
            if verbose:
                print('Unknown Z')
            return 'unknown'    
        
        # Z-I :     
        ZmI = z - int(i_str)
        conf = ('s1', 's2', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6','d7', 'd8')
        cn = np.array([2, 10, 28, 46, 54, 78])
        #cn = np.array([2, 10, 18, 36, 54, 86])
        k = ZmI - cn
        try:
            k_res = np.min(k[k>=0])
            res = conf[k_res]
            if verbose:
                print('Obtained from formula')
        except:
            if verbose:
                print('Unknown conf')
            res = 'unknown'

    
    return res 

def make_gsconf_file(outfile='gsconfs.dat'):
    file_names = sorted(glob('*levels.dat'))
    with open(outfile, 'w') as of:
        for fname in file_names:
            elem, ion, foo = fname.split('_')
            elem = elem.capitalize()

            ion = int(roman_to_int(ion))