def get_slurm_header(slurm_dict, walltime_hrs, num_nodes=None):
    """ Write a SLURM script header from a set of slurm parameters.

    Parameters:
        slurm_dict (dict): dictionary of SLURM environment variables.
        walltime_hrs (int): allowed walltime in hours

    Keyword arguments:
        num_nodes (int): overrides $SLURM_JOB_NUM_NODES with a custom value.

    Returns:
        header (str): the SLURM file header.

    """

    header = "#!/bin/bash\n"
    header += "#! SLURM file written by matador (Matthew Evans 2016-2018).\n\n"
    header += "#! Name of job:\n"
    header += "#SBATCH --job-name {}\n".format(slurm_dict['SLURM_JOB_NAME'])
    header += "#! Name of project:\n"
    header += "#SBATCH --account {}\n".format(slurm_dict['SLURM_JOB_ACCOUNT'])
    if num_nodes is None:
        num_nodes = slurm_dict['SLURM_JOB_NUM_NODES']
    header += "#! Number of nodes to allocate:\n"
    header += "#SBATCH --nodes {}\n".format(num_nodes)
    header += "#! Number of tasks to allocate:\n"
    header += "#SBATCH --ntasks {}\n".format(slurm_dict['SLURM_NTASKS'])
    header += "#! Partition:\n"
    header += "#SBATCH --partition {}\n".format(slurm_dict['SLURM_JOB_PARTITION'])
    header += "#! Walltime to allocate:\n"
    header += "#SBATCH --time {}:00:00\n".format(walltime_hrs)

    return header