# Path: orbkit/units.py

# 
# Path: orbkit/display.py
# def display(string):
#   '''Prints :literal:`string` to the terminal output and to the .oklog file.'''
#   global log_fid
#   if not options.quiet:
#     print(string)
#   if not options.no_log:
#     if log_fid is None:
#       if options.outputname is None or options.outputname == '':
#         return
#       else:
#         log_fid = '%s.oklog' % options.outputname.split('@')[0]
#     if not is_initiated:
#       init_display(log_fid)
#     f = open(log_fid, 'a')
#     f.write('%s\n' % string)
#     f.close()
# 
# Path: orbkit/qcinfo.py
# class CIinfo():
#   '''Class managing all information from the from the output
#   files of quantum chemical software for CI calculations.
# 
#   The CI related features are in ongoing development.
#   '''
#   def __init__(self,method='ci'):
#     self.method = method
#     self.info   = None
#     self.coeffs = []
#     self.occ    = []
#     self.moocc  = None
# 
#   def __str__(self):
#     string = '%s' % self.method.upper()
#     if self.info is not None:
#       string += ' State %(state)s' % self.info
#       if 'spin' in self.info.keys() and self.info['spin'] != 'Unknown':
#          string += ' (%(spin)s)' % self.info
#     if numpy.shape(self.coeffs) != (0,):
#       string += ':\tNorm = %0.8f (%d Coefficients)' %(self.get_norm(),
#                                                       len(self.coeffs))
#     return  string
#   def __eq__(self, other):
#     try:
#       return self.__dict__ == other.__dict__
#     except ValueError:
#       return False
#   def get_norm(self):
#     return sum(self.coeffs**2)
#   def renormalize(self):
#     self.coeffs /= self.get_norm()
#   def apply_threshold(self,threshold,keep_length=False):
#     i = numpy.abs(self.coeffs) > threshold
#     if keep_length:
#       self.coeffs[numpy.invert(i)] = 0.0
#     else:
#       if self.info['state'] == '0':
#        self.coeffs = self.coeffs[i]
#        self.occ = self.occ[:1]
#       else:
#        self.coeffs = self.coeffs[i]
#        self.occ = self.occ[i]
#       
#   def copy(self):
#     ciinfo = deepcopy(self)
#     return ciinfo
# 
#   def get_moocc(self):
#     if self.moocc is None:
#       raise ValueError('ci.set_moocc(qc) has to be called first! (ci.moocc is not initialized)')
#     return self.moocc
# 
#   def set_moocc(self,moocc):
#     assert (moocc.dtype == numpy.intc), 'moocc has to be numpy.intc'
#     self.moocc = moocc
# 
#   def hdf5_save(self,fid='out.h5',group='/ci:0',mode='w'):
#     from orbkit.output import hdf5_open,hdf5_append
#     from copy import copy
#     for hdf5_file in hdf5_open(fid,mode=mode):
#       dct = copy(self.todict())
#       dct['info'] = numpy.array(dct['info'].items(),dtype=str)
#       hdf5_append(dct,hdf5_file,name=group)
# 
#   def hdf5_read(self,fid='out.h5',group='/ci:0'):
#     from orbkit.output import hdf5_open,hdf52dict
#     for hdf5_file in hdf5_open(fid,mode='r'):
#       for key in self.__dict__.keys():
#         try:
#           self.__dict__[key] = hdf52dict('%s/%s' % (group,key),hdf5_file)
#         except KeyError:
#           self.__dict__[key] = hdf5_file['%s' % group].attrs[key]
#       self.__dict__['info'] = dict(self.__dict__['info'])
# 
# Path: orbkit/read/tools.py
# def descriptor_from_file(filename, index=0, ci_descriptor=False):
#   from .tar import is_tar_file, get_file_from_tar
# 
#   if is_tar_file(filename):
#     fname, _ = get_file_from_tar(filename, index=index, ci_descriptor=ci_descriptor)
#   elif os.path.splitext(filename)[1] == '.gz':
#     fname = gzip.open(filename, 'rt')
#   else:
#     fname = open(filename, 'r')
#   return fname
# 
# Path: orbkit/detci/ci_read/tools.py
# def multiplicity():
#   multiplicity = ['Unknown','Singlet','Doublet','Triplet','Quartet','Quintet']
#   return multiplicity


import numpy
from copy import copy
from orbkit.units import ev_to_ha
from orbkit.display import display
from orbkit.qcinfo import CIinfo
from orbkit.read.tools import descriptor_from_file
from .tools import multiplicity



def gamess_tddft(fname,select_state=None,threshold=0.0,**kwargs):
  '''Reads GAMESS-US TDDFT output. 
  
  **Parameters:**
  
    fname: str, file descriptor
      Specifies the filename for the input file.
      fname can also be used with a file descriptor instad of a filename.
    select_state : None or list of int, optional
      If not None, specifies the states to be read (0 corresponds to the ground 
      state), else read all electronic states.
    threshold : float, optional
      Specifies a read threshold for the CI coefficients.
  
  **Returns:**
  
    ci : list of CIinfo class instances
      See :ref:`Central Variables` for details.
  '''

# Path: orbkit/units.py

# 
# Path: orbkit/display.py
# def display(string):
#   '''Prints :literal:`string` to the terminal output and to the .oklog file.'''
#   global log_fid
#   if not options.quiet:
#     print(string)
#   if not options.no_log:
#     if log_fid is None:
#       if options.outputname is None or options.outputname == '':
#         return
#       else:
#         log_fid = '%s.oklog' % options.outputname.split('@')[0]
#     if not is_initiated:
#       init_display(log_fid)
#     f = open(log_fid, 'a')
#     f.write('%s\n' % string)
#     f.close()
# 
# Path: orbkit/qcinfo.py
# class CIinfo():
#   '''Class managing all information from the from the output
#   files of quantum chemical software for CI calculations.
# 
#   The CI related features are in ongoing development.
#   '''
#   def __init__(self,method='ci'):
#     self.method = method
#     self.info   = None
#     self.coeffs = []
#     self.occ    = []
#     self.moocc  = None
# 
#   def __str__(self):
#     string = '%s' % self.method.upper()
#     if self.info is not None:
#       string += ' State %(state)s' % self.info
#       if 'spin' in self.info.keys() and self.info['spin'] != 'Unknown':
#          string += ' (%(spin)s)' % self.info
#     if numpy.shape(self.coeffs) != (0,):
#       string += ':\tNorm = %0.8f (%d Coefficients)' %(self.get_norm(),
#                                                       len(self.coeffs))
#     return  string
#   def __eq__(self, other):
#     try:
#       return self.__dict__ == other.__dict__
#     except ValueError:
#       return False
#   def get_norm(self):
#     return sum(self.coeffs**2)
#   def renormalize(self):
#     self.coeffs /= self.get_norm()
#   def apply_threshold(self,threshold,keep_length=False):
#     i = numpy.abs(self.coeffs) > threshold
#     if keep_length:
#       self.coeffs[numpy.invert(i)] = 0.0
#     else:
#       if self.info['state'] == '0':
#        self.coeffs = self.coeffs[i]
#        self.occ = self.occ[:1]
#       else:
#        self.coeffs = self.coeffs[i]
#        self.occ = self.occ[i]
#       
#   def copy(self):
#     ciinfo = deepcopy(self)
#     return ciinfo
# 
#   def get_moocc(self):
#     if self.moocc is None:
#       raise ValueError('ci.set_moocc(qc) has to be called first! (ci.moocc is not initialized)')
#     return self.moocc
# 
#   def set_moocc(self,moocc):
#     assert (moocc.dtype == numpy.intc), 'moocc has to be numpy.intc'
#     self.moocc = moocc
# 
#   def hdf5_save(self,fid='out.h5',group='/ci:0',mode='w'):
#     from orbkit.output import hdf5_open,hdf5_append
#     from copy import copy
#     for hdf5_file in hdf5_open(fid,mode=mode):
#       dct = copy(self.todict())
#       dct['info'] = numpy.array(dct['info'].items(),dtype=str)
#       hdf5_append(dct,hdf5_file,name=group)
# 
#   def hdf5_read(self,fid='out.h5',group='/ci:0'):
#     from orbkit.output import hdf5_open,hdf52dict
#     for hdf5_file in hdf5_open(fid,mode='r'):
#       for key in self.__dict__.keys():
#         try:
#           self.__dict__[key] = hdf52dict('%s/%s' % (group,key),hdf5_file)
#         except KeyError:
#           self.__dict__[key] = hdf5_file['%s' % group].attrs[key]
#       self.__dict__['info'] = dict(self.__dict__['info'])
# 
# Path: orbkit/read/tools.py
# def descriptor_from_file(filename, index=0, ci_descriptor=False):
#   from .tar import is_tar_file, get_file_from_tar
# 
#   if is_tar_file(filename):
#     fname, _ = get_file_from_tar(filename, index=index, ci_descriptor=ci_descriptor)
#   elif os.path.splitext(filename)[1] == '.gz':
#     fname = gzip.open(filename, 'rt')
#   else:
#     fname = open(filename, 'r')
#   return fname
# 
# Path: orbkit/detci/ci_read/tools.py
# def multiplicity():
#   multiplicity = ['Unknown','Singlet','Doublet','Triplet','Quartet','Quintet']
#   return multiplicity


# Path: orbkit/detci/ci_read/gamess.py
import numpy
from copy import copy
from orbkit.units import ev_to_ha
from orbkit.display import display
from orbkit.qcinfo import CIinfo
from orbkit.read.tools import descriptor_from_file
from .tools import multiplicity




def gamess_tddft(fname,select_state=None,threshold=0.0,**kwargs):
  '''Reads GAMESS-US TDDFT output. 
  
  **Parameters:**
  
    fname: str, file descriptor
      Specifies the filename for the input file.
      fname can also be used with a file descriptor instad of a filename.
    select_state : None or list of int, optional
      If not None, specifies the states to be read (0 corresponds to the ground 
      state), else read all electronic states.
    threshold : float, optional
      Specifies a read threshold for the CI coefficients.
  
  **Returns:**
  
    ci : list of CIinfo class instances
      See :ref:`Central Variables` for details.
  '''

  display('\nReading data of TDDFT calculation from GAMESS-US...')