def generate_file(file, geomtype, geom, commsec, jobsec):
    """Construct input file from options of all jobs.
    file: input file path
    geomtype: absolute or incremental
    geom: geometry points
    commsec: common section settings
    jobsec: list of job settings"""
    B = {(True): '.T.', (False): '.F.', None: '', 'Absolute': '##',
        'Incremental': '@@'}
    try:
        with open(file, 'w') as f:
            f.write(
                f""" &FILE LSAV={B[commsec['gensec']['LSAV']]}, ITEST={commsec['gensec']['ITEST']}, LREC={B[commsec['gensec']['LSAV']]}, LCPUTM={B[commsec['gensec']['LSAV']]}, TSOS={commsec['gensec']['TSOS']}, TMAX={commsec['gensec']['TMAX']} &END
"""
                )
            f.write(f" {commsec['gensec']['TITLE']}\n")
            f.write(' &BOUN IZL=3, IZR=3 &END\n')
            f.write(f" &MESH DDZ={jobsec[0]['meshsec']['DDZ']}")
            if jobsec[0]['meshsec']['DDR'] is not None:
                f.write(f", DDR={jobsec[0]['meshsec']['DDR']}")
            if jobsec[0]['meshsec']['NR'] is not None:
                f.write(f", NR={jobsec[0]['meshsec']['NR']}")
            if jobsec[0]['meshsec']['NZ'] is not None:
                f.write(f", NZ={jobsec[0]['meshsec']['NZ']}")
            if jobsec[0]['meshsec']['RMARK'] is not None:
                f.write(f", RMARK={jobsec[0]['meshsec']['RMARK']}")
            if jobsec[0]['meshsec']['RZ'] is not None:
                f.write(f", RZ={jobsec[0]['meshsec']['RZ']}")
            f.write(' &END\n')
            f.write(f' {B[geomtype]}CAVITYSHAPE\n')
            f.write('0.\n')
            for r, z in geom:
                f.write(f'{r} {z}\n')
            f.write('9999. 9999.\n')
            firstpass = True
            for j in jobsec:
                if not firstpass:
                    f.write('CONTINUE\n')
                    f.write(f' &MESH')
                    if j['meshsec']['DDZ'] is not None:
                        f.write(f" DDZ={j['meshsec']['DDZ']}")
                    else:
                        f.write(f' DDZ=0.001')
                    if j['meshsec']['DDR'] is not None:
                        f.write(f", DDR={j['meshsec']['DDR']}")
                    else:
                        f.write(f', DDR=0.001')
                    if j['meshsec']['NR'] is not None:
                        f.write(f", NR={j['meshsec']['NR']}")
                    if j['meshsec']['NZ'] is not None:
                        f.write(f", NZ={j['meshsec']['NZ']}")
                    if j['meshsec']['RMARK'] is not None:
                        f.write(f", RMARK={j['meshsec']['RMARK']}")
                    if j['meshsec']['RZ'] is not None:
                        f.write(f", RZ={j['meshsec']['RZ']}")
                    f.write(' &END\n')
                f.write(
                    f" &BEAM MROT={j['beamsec']['MROT']}, ISIG={j['beamsec']['ISIG']}, NBUNCH={j['beamsec']['NBUNCH']}, BSEP={j['beamsec']['BSEP']}"
                    )
                if j['beamsec']['SIG'] is not None:
                    f.write(f", SIG={j['beamsec']['SIG']}")
                if j['beamsec']['RDRIVE'] is not None:
                    f.write(f", RDRIVE={j['beamsec']['RDRIVE']}")
                f.write(' &END\n')
                if firstpass:
                    f.write(
                        f" &TIME MT={commsec['timesec']['MT']}, NSHOT={commsec['timesec']['NSHOT']}"
                        )
                    if commsec['timesec']['TPS'] is not None:
                        f.write(f", TPS={commsec['timesec']['TSP']}")
                    f.write(' &END\n')
                f.write(
                    f" &WAKE LCFRON={j['wakesec']['LCFRON']}, LCBACK={j['wakesec']['LCBACK']}, LCHIN={j['wakesec']['LCHIN']}, LNAPOLY={j['wakesec']['LNAPOLY']}, LNONAP={j['wakesec']['LNONAP']}, LCRBW={j['wakesec']['LCRBW']}, ZSEP={j['wakesec']['ZSEP']}"
                    )
                if j['wakesec']['UBT'] is not None:
                    f.write(f", UBT={j['wakesec']['UBT']}")
                if j['wakesec']['ZCF'] is not None:
                    f.write(f", ZCF={j['wakesec']['ZCF']}")
                if j['wakesec']['ZCT'] is not None:
                    f.write(f", ZCT={j['wakesec']['ZCT']}")
                if j['wakesec']['RWAK'] is not None:
                    f.write(f", RWAK={j['wakesec']['RWAK']}")
                f.write(' &END\n')
                f.write(
                    f""" &PLOT LCAVIN={j['plotsec']['LCAVIN']}, LCAVUS={j['plotsec']['LCAVUS']}, LPATH={j['plotsec']['LPATH']}, LPLW={j['plotsec']['LPLW']}, LPLWL={j['plotsec']['LPLWL']}, LPLWA={j['plotsec']['LPLWA']}, LPLWT={j['plotsec']['LPLWT']}, LFFT={j['plotsec']['LFFT']}, LFFTA={j['plotsec']['LFFTA']}, LFFTA={j['plotsec']['LFFTA']}, LFFTT={j['plotsec']['LFFTT']}, LINTZ={j['plotsec']['LINTZ']}, LWNDW={j['plotsec']['LWNDW']}, NWFUN={j['plotsec']['NWFUN']}, ALPHA={j['plotsec']['ALPHA']}, CUTOFF={j['plotsec']['CUTOFF']}, EXPFAC={j['plotsec']['EXPFAC']}, LPLE={j['plotsec']['LPLE']}, LPLC={j['plotsec']['LPLC']}, NPLOT={j['plotsec']['NPLOT']}, LPALL={j['plotsec']['LPALL']} &END
"""
                    )
                f.write(
                    f""" &PRIN LPRW={j['printsec']['LPRW']}, LMATPR={j['printsec']['LMATPR']}, LSVW={j['printsec']['LSVW']}, LSVWL={j['printsec']['LSVWL']}, LSVWA={j['printsec']['LSVWA']}, LSVWT={j['printsec']['LSVWT']}, LSVF={j['printsec']['LSVF']} &END
"""
                    )
                firstpass = False
            f.write('STOP\n')
    except PermissionError:
        pass
