def check_freq(job):
    """
    Check if a freq job is done or crashed, if done: check for imaginary and
    send for adjustment if required.
    Return a status string
    """

    if job.freqopt == 'numforce':
        filetoread = os.path.join(job.indir, 'numforce', 'aoforce.out')
        endfile = 'numforce.out'
    else:
        filetoread = os.path.join(job.indir, 'aoforce.out')
        endfile = 'aoforce.out'

    try:
        with open(filetoread, 'r') as f:
            endstatus = f.readlines()[-5]
    except (OSError, IOError) as e:
        endstatus = ''
        logging.info(
            "Error {} reading aoforce.out for {}".format(
                e, job.indir))

    else:
        if "   ****  force : all done  ****" in endstatus:

            freqtime = turbogo_helpers.get_calc_time(job.indir, endfile)
            if freqtime:
                job.ftime += freqtime
            else:
                job.ftime += (time() - job.curstart)

            logging.info('Job {} completed frequency.'.format(job.name))
            job.status = "Completed Freq"
            if not job.ts:
                status = ensure_not_ts(job)
            else:
                status = ensure_ts(job)
            if status == 'completed':
                if job.freeh:
                    logging.debug('Do Freeh')
                    do_freeh(job)
                return 'completed'
            elif status == 'error':
                return 'ocrashed'
            elif status == 'opt':
                return 'opt'
            elif status == 'same':
                return 'same'
            elif status == 'imaginary':
                return 'imaginary'
            elif status == 'ts':
                return 'ts'
        else:
            job.status = "Freq Crashed"
            return 'fcrashed'
def check_opt(job):
    """
    Check if an opt job is done or crashed, if done: resubmit to queue if freq
    is required. Return a status string
    """
    if turbogo_helpers.check_files_exist([
        os.path.join(job.indir, 'GEO_OPT_CONVERGED')]):
        #Job converged
        logging.info('Job {} completed optimization.'.format(job.name))
        if job.jobtype == 'opt' or job.jobtype == 'optfreq':
            opttime = turbogo_helpers.get_calc_time(job.indir, 'opt.out')
        elif job.jobtype == 'ts':
            opttime = turbogo_helpers.get_calc_time(job.indir, 'ts.out')
        elif job.jobtype == 'sp':
            opttime = turbogo_helpers.get_calc_time(job.indir, 'sp.out')
        else:
            opttime = False
        if opttime:
            job.otime += opttime
        else:
            job.otime += (time() - job.curstart)

        if job.freqopt != None:
            newid = freq_submit(job)
            if newid != -99:
                job.curstart = time()
                job.jobid = newid
                job.status = "Freq Submitted"
                return 'freq'
            else:
                job.status = "Freq Setup Failed"
                return 'fcrashed'
        else:
            if reformat:
                convert_filetype(os.path.join(job.indir, 'finalgeometry.xyz'),
                                 os.path.join(job.indir, 'finalgeometry.mol'))
            return 'completed'

    else:
        logging.warning("Job {} crashed in optimization."
                     .format(job.name))
        job.status = "Opt Crashed"
        return 'ocrashed'