DoThreeRDM')
            self.extraline.append('DoFourRDM')

            writeSHCIConfFile(self, nelec, False)
            if self.verbose >= logger.DEBUG1:
                inFile = self.configFile
                #inFile = os.path.join(self.scratchDirectory,self.configFile)
                logger.debug1(self, 'SHCI Input conf')
                logger.debug1(self, open(inFile, 'r').read())

            start = time.time()
            mpisave = self.mpiprefix
            #self.mpiprefix=""
            executeSHCI(self)
            self.mpiprefix = mpisave
            end = time.time()
            print('......production of RDMs took %10.2f sec' % (end - start))
            sys.stdout.flush()

            if self.verbose >= logger.DEBUG1:
                outFile = self.outputFile
                #outFile = os.path.join(self.scratchDirectory,self.outputFile)
                logger.debug1(self, open(outFile).read())
            self.has_fourpdm = True
            self.has_threepdm = True
            self.extraline.pop()

        if (bypass): return None

        # The 4RDMS binary files written by "SHCIrdm::save3RDM" in DICE
        # are written as E4[i1,j2,k3,l4,m4,n3,o2,p1]
        # and are stored here as E4[i1,j2,k3,l4,p1,o2,n3,m4]
        # This is done with SQA in mind.
        start = time.time()
        if (filetype == "binary"):
            print('Reading binary 4RDM from DICE')
            fname = os.path.join(self.scratchDirectory,
                                 "spatial4RDM.%d.%d.bin" % (state, state))
            E4 = self.unpackE4_DICE(fname, norb)

        # The 4RDMs text files written by "SHCIrdm::save3RDM" in DICE
        # are written as E4[i1,j2,k3,l4,m4,n3,o2,p1]
        # and are stored here as E4[i1,j2,k3,l4,p1,o2,n3,m4]
        # This is done with SQA in mind.
        else:
            print('Reading text-file 4RDM')
            fname = os.path.join(self.scratchDirectory,
                                 "spatial4RDM.%d.%d.txt" % (state, state))
            f = open(fname, 'r')
            lines = f.readlines()
            E4 = numpy.zeros(
                shape=(norb, norb, norb, norb, norb, norb, norb, norb),
                dtype=dt,
                order='F')
            assert (int(lines[0]) == norb)
            for line in lines[1:]:
                linesp = line.split()
                if (len(linesp)!= 9):
                    continue
                a,b,c,d, e,f,g,h, integral = int(linesp[0]), int(linesp[1]), int(linesp[2]), int(linesp[3]),\
                                             int(linesp[4]), int(linesp[5]), int(linesp[6]), int(linesp[7]), float(linesp[8])
                self.populate(E4, [a,b,c,d,  h,g,f,e], integral)
        end = time.time()
        print('......reading the RDM took    %10.2f sec' % (end - start))
        print('')
        sys.stdout.flush()
        return E4

    def populate(self, array, list, value):
        dim = len(list) / 2
        up = list[:dim]
        dn = list[dim:]
        import itertools
        for t in itertools.permutations(range(dim), dim):
            updn = [up[i] for i in t] + [dn[i] for i in t]
            array[tuple(updn)] = value

    def unpackE2_DICE(self,fname,norb):
        # The 2RDMs written by "SHCIrdm::saveRDM" in DICE
        # are written as E2[i1,j2,k2,l2]
        # and are stored here as E2[i1,j2,l2,k2]
        # This is done with SQA in mind.
        fil=open(fname,"rb")
        print("     [fil.seek: How dangerous is that??]")
        fil.seek(53)  # HOW DANGEROUS IS THAT???!?!?!?
        spina=-1
        spinb=-1
        spinc=-1
        spind=-1
        ab='ab'
        E2hom=numpy.zeros((norb,norb,norb,norb), order='F')
        E2het=numpy.zeros((norb,norb,norb,norb), order='F')
        for a in range(2*norb):
            spina=(spina+1)%2
            for b in range(a+1):
                spinb=(spinb+1)%2
                for c in range(2*norb):
                    spinc=(spinc+1)%2
                    for d in range(c+1):
                        spind=(spind+1)%2
                        A,B,C,D=int(a/2.),int(b/2.),int(c/2.),int(d/2.)
                        (value,)=struct.unpack('d',fil.read(8))
                        if spina==spinb and spina==spinc and spina==spind:
                           #print '%3i%3i%3i%3i %3i%3i%3i%3i %1s%1s%1s%1s E2hom %13.5e'%(a,b,c,d,A,B,C,D,ab[spina],ab[spinb],ab[spinc],ab[spind],value)
                            if (a%2==d%2 and b%2==c%2):
                                E2hom[A,B, D,C]-=value
                                E2hom[B,A, C,D]-=value
                            if (a%2==c%2 and b%2==d%2) and (a!=b):
                                E2hom[A,B, C,D]+=value
                                E2hom[B,A, D,C]+=value
                        elif (spina==spinb and spinc==spind)\
                           or(spina==spinc and spinb==spind)\
                           or(spina==spind and spinb==spinc):
                           #print '%3i%3i%3i%3i %3i%3i%3i%3i %1s%1s%1s%1s E2het %13.5e'%(a,b,c,d,A,B,C,D,ab[spina],ab[spinb],ab[spinc],ab[spind],value)
                            if (a%2==d%2 and b%2==c%2):
                                E2het[A,B, D,C]-=value
                                E2het[B,A, C,D]-=value
                            if (a%2==c%2 and b%2==d%2) and (a!=b):
                                E2het[A,B, C,D]+=value
                                E2het[B,A, D,C]+=value
                       #else:
                       #    print '%3i%3i%3i%3i %3i%3i%3i%3i %1s%1s%1s%1s NONE  %13.5e'%(a,b,c,d,A,B,C,D,ab[spina],ab[spinb],ab[spinc],ab[spind],value)
                    spind=-1
                spinc=-1
            spinb=-1
        spina=-1
        try:
            (value,)=struct.unpack('c',