# 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
        print('{:<5}'.format(elem), ' '.join(['{:6.2f}'.format(ip) for ip in IP[elem][0:N_ions]])) 

def gsFromAtom(atom, verbose=False):
    """
    atom: eg 'O3' for O++
    """
    """
    special_dict = {'Fe2': 'd7', 'Fe3': 'd6', 'Fe4': 'd5', 'Fe5': 'd4', 'Fe6': 'd3', 'Fe7': 'd2', 'Ni3': 'd8'
                    }
    if atom in special_dict:
        return special_dict[atom]
    """
    # The import needs to be here. If in the header, infinite loop occurs at start

    if atom in pn.atomicData.gsconf:
        if verbose:
            print('Obtained from gsconfs.dat')
        return pn.atomicData.gsconf[atom]
        
    res = 'unknown'
    try:
        NIST_gsconf = getLevelsNIST(atom)[0][0].split('.')[-1]
        if NIST_gsconf[-1] in ('0123456789'):
            NIST_gsconf = NIST_gsconf[-2:]
        else:
            NIST_gsconf = NIST_gsconf[-1] + '1'
        res = NIST_gsconf
        if verbose:
            print('Obtained from NIST')
    except:

# 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

        print('{:<5}'.format(elem), ' '.join(['{:6.2f}'.format(ip) for ip in IP[elem][0:N_ions]])) 

def gsFromAtom(atom, verbose=False):
    """
    atom: eg 'O3' for O++
    """
    """
    special_dict = {'Fe2': 'd7', 'Fe3': 'd6', 'Fe4': 'd5', 'Fe5': 'd4', 'Fe6': 'd3', 'Fe7': 'd2', 'Ni3': 'd8'
                    }
    if atom in special_dict:
        return special_dict[atom]
    """
    # The import needs to be here. If in the header, infinite loop occurs at start

    if atom in pn.atomicData.gsconf:
        if verbose:
            print('Obtained from gsconfs.dat')
        return pn.atomicData.gsconf[atom]
        
    res = 'unknown'
    try:
        NIST_gsconf = getLevelsNIST(atom)[0][0].split('.')[-1]
        if NIST_gsconf[-1] in ('0123456789'):
            NIST_gsconf = NIST_gsconf[-2:]
        else:
            NIST_gsconf = NIST_gsconf[-1] + '1'
        res = NIST_gsconf
        if verbose:
            print('Obtained from NIST')
    except:

        elem, i_str = parseAtom(atom) # Tranform e.g. 'O3' into 'O', '3'