Index: database/import_gaussian_calc
===================================================================
--- database/import_gaussian_calc	(revision 45)
+++ database/import_gaussian_calc	(revision 46)
@@ -2,9 +2,10 @@
 # $Id$
 import os, sys, argparse
 from ase import Atoms
-from ase.io import read
+from ase.io.gaussian import read_gaussian_out
 from ase.db import connect
 from ase.calculators.gaussian import Gaussian
+from ase.calculators.emt import EMT
 from ase.constraints import FixAtoms
 from numpy.linalg import norm
 from bz2 import BZ2File
@@ -35,7 +36,7 @@
         # It was fine, give a normal answer
         return result
 
-def import_gaussian_calculations(path,db,include_dir=[],exclude_dir=[],use_input=False,creator=os.environ['USER'], nproc=8):
+def import_gaussian_calculations(path,db,include_dir=[],exclude_dir=[],use_input=False,project='gaussian',user=os.environ['USER'], nproc=8):
     #log = open('scan.log','w')
     _all = os.walk(os.path.abspath(path))
     calculator=None
@@ -44,7 +45,7 @@
     filenames = []
     if os.path.exists(db):
         con = connect(db)
-        for row in con.select(code='GAUSSIAN'):
+        for row in con.select(project='test'):
             filenames.append(row.filename)
     else:
         con = connect(db)
@@ -57,11 +58,11 @@
         condition1 = (read_all_dir or a[0].split('/')[-1] in include_dir)
         #condition2 = not files[0].split('/')[-1] in exclude_dir
         condition3 = [ i for i, x in enumerate(exclude_dir) if x in a[0] ] 
-        _gass_out = [i for i in range(len(a[2])) if ('.log' in a[2][i] and a[2][i][-4:]=='.log')]
+        _gass_out = [i for i in range(len(a[2])) if ('.log' in a[2][i])]
         if condition1 and not condition3:
             for f in _gass_out:
-                filetoread = a[2][f]
                 path = a[0]
+                filetoread = path +'/'+ a[2][f]
                 if not filetoread in filenames:
                     all_files.append(filetoread)
                     paths.append(path)
@@ -69,119 +70,46 @@
     multiprocessing.log_to_stderr()
     print 'using '+str(nproc)+' threads'
     pool = multiprocessing.Pool(nproc)
-    results = []
     for filetoread,path in zip(all_files, paths):
-        #result = pool.apply_async(LogExceptions(mp_worker), args=(con,files,use_input,filenames,xc_dict), callback=log_result)
-        pool.apply_async(LogExceptions(mp_worker), args=(con,filetoread,path,use_input,filenames,xc_dict), callback=log_result)
+        #def callback(result, func=LogExceptions(mp_worker)):
+        #    results[func] = result
+        result = pool.apply_async(LogExceptions(mp_worker), args=(con,files,use_input,filenames,xc_dict), callback=log_result)
     pool.close()
     pool.join()
     t3 = time()
+    con = connect(db)
     print 'total time used: ', t3-t2, ' seconds'
 
 def log_result(log):
     logfile = open('scan.log','a')
     print >> logfile, log
+    return log
 
-def mp_worker(con, filetoread, path, use_input,filenames,xc_dict):
+def mp_worker(con, filetoread, path, use_input,filenames, user, project):
     try:
         t1 = time()
-        filetoread = path +'/'+ filetoread
-        Atoms = read(filetoread)
+        Atoms, data = read_gaussian_out(filetoread,quantity='all')
         t2 = time()
         print 'timing: ', t2-t1
-        if use_input:
-            pass
-            #constraints = read(path+'/input').constraints
-            #Atoms.set_constraint(constraints)
-            #print 'get constraint from input', filetoread
-        else:
-            Atoms0 = read(filetoread, index=0)
-            pos0 = Atoms0.positions
-            pos = Atoms.positions
-            diff = pos - pos0
-            disp = norm(diff, axis=1)
-            if disp.sum() > 0:
-                print 'find constraint from outcar'
-                mask = [_disp == 0 for _disp in disp]
-                constraints = FixAtoms(mask=mask)
-                Atoms.set_constraint(constraints)
-            else:
-                print 'no constraint'
-                constraints = None
 
-        #read version number, XC, ENCUT etc.
-        if filetoread[-4:] == '.bz2':
-            fobj = BZ2File(filetoread)
-        elif filetoread[-3:] == '.gz':
-            fobj = GzipFile(filetoread)
-        else:
-            fobj = open(filetoread)
-        with fobj as outcar:
-            version = outcar.readline()
-            line = outcar.readline()
-            read_para = 0
-            lsol = False
-            ldipol = False
-            while line != '':
-                if line.startswith(' INCAR:'):
-                    line = outcar.readline().split()
-                    if line[1] == 'PAW_PBE':
-                        pot = 'PAW'
-                    else:
-                        pot = line[1]
-                elif line.startswith(' Dimension of arrays'):
-                #if line.startswith(' Dimension of arrays'):
-                    read_para = 1
-                elif read_para:
-                    if 'LEXCH' in line:
-                        xc_flag = line.split()[2].upper()
-                        if xc_flag not in xc_dict.keys():
-                            raise ValueError('Unknown xc-functional flag found in POTCAR,'
-                                             ' LEXCH=%s' % xc_flag)
-                        xc = xc_dict[xc_flag]
-                    elif 'NKPTS' in line:
-                    #if 'NKPTS' in line:
-                        nkpts = int(line.split()[3])
-                    elif 'ENCUT' in line:
-                        encut = float(line.split()[2])
-                    elif 'ENAUG' in line:
-                        enaug = float(line.split()[2])
-                    elif 'NBANDS' in line:
-                        nbands = int(line.split()[-1])
-                    elif 'EDIFFG' in line:
-                        f_limit = -float(line.split()[2])
-                    elif 'LSOL' in line:
-                        if line.split()[2] == 'T':
-                            lsol = True
-                    elif 'LDIPOL' in line:
-                        if line.split()[2] == 'T':
-                            ldipol = True
-                    elif '-'*104 in line:
-                        break
-                line = outcar.readline()
-        if constraints is not None:
-            fmax = ((Atoms.get_forces()) ** 2).sum(1).max()**0.5
-            if fmax < f_limit:
-                #print >> log, 'updating the database with minimized calculation from file: ', fmax, files[2][f]
-                _log = 'archived_a: '+filetoread+' '+str(fmax)
-                #if use_input:
-                #print os.getenv('USER')
-                id = con.reserve(filename=filetoread)
-                con.write(Atoms,functional = xc,path=path,code='GAUSSIAN',filename=filetoread, version=version, potential=pot, encut=encut, enaug=enaug, lsol=lsol, ldipol=ldipol, nkpts=nkpts, constraint=True)
-                del con[id]
-            else:
-                #print >> log, 'not updating the database as fmax is too big for file: ', fmax, files[2][f]
-                _log = 'skipped: '+filetoread+' '+str(fmax)
-        else:
-            #print >> log, 'updating the database with calculation', files[2][f]
-            _log = 'archived_b: '+filetoread
-            id = con.reserve(filename=filetoread)
-            con.write(Atoms,functional = xc,creator=creator,path=path,code='GAUSSIAN',filename=filetoread, version=version, potential=pot, encut=encut, enaug=enaug, lsol=lsol, ldipol=ldipol, nkpts=nkpts, constraint=False)
-            del con[id]
+        _log = 'archived_a: '+filetoread
+        id = con.reserve(filename=filetoread)
+        con.write(Atoms,functional = data['Method'],charge=data['Charge'],basis_set=data['Basis_set'],path=path,filename=filetoread, version=data['Version'],project='test')
+        del con[id]
+        record = con.select(filename=filetoread)
+#==================================
+#This part will only exist temporarily, it is for importing other people's calculation
+#For update the user and calculator keys, tjiang's hacked copy of ase is needed, 
+#as these two are reserved keys that are not allowed to be updated manually
+        id = next(record)['id'] 
+        con.update(id,user='edumont')
+#==================================
+        con.update(id,calculator='gaussian')
+        print _log 
     except (IndexError, ValueError):
         _log = 'failed: '+filetoread
-    print _log 
-    #_log.flush()
+        print _log 
+    ##_log.flush()
     return
 
 if __name__ == '__main__':
@@ -192,6 +120,7 @@
     parser.add_argument("-e", "--exclude_dir",help="Directory to be excluded in the scanning")
     parser.add_argument("-d", help="Name of the database file to store the scanned calculations")
     parser.add_argument("-n", help="Number of processers")
+    parser.add_argument("-j", help="Project name")
     args = parser.parse_args()
     if args.path is None:
         path = '.'
@@ -217,5 +146,9 @@
         nproc = 8
     else:
         nproc = int(args.n)
+    if args.j is None:
+        project = 'gaussian'
+    else:
+        project = int(args.j)
 
-    import_gaussian_calculations(path, db, use_input=use_input, include_dir=include_dir, exclude_dir=exclude_dir, nproc=nproc)
+    import_gaussian_calculations(path, db, use_input=use_input, include_dir=include_dir, exclude_dir=exclude_dir, project=project, nproc=nproc)
