def add_key_val(keyname, keyval, keytype, filename, extnum):
    """Add/replace FITS key

    Add/replace the key keyname with value keyval of type keytype in filename.

    Parameters:
    ----------
    keyname : str
        FITS Keyword name.
    keyval : str
        FITS keyword value.
    keytype: str
        FITS keyword type: int, float, str or bool.
    filaname : str
        FITS filename.
    extnum : int
        Extension number where the keyword will be inserted. Note that
        the first extension is number 1 (and not zero).

    """

    funtype = {'int': int, 'float': float, 'str': str, 'bool': bool}
    if keytype not in funtype:
        raise ValueError('Undefined keyword type: ', keytype)
    with fits.open(filename, "update") as hdulist:
        hdulist[extnum].header[keyname] = funtype[keytype](keyval)
        print('>>> Inserting ' + keyname + '=' + keyval + ' in ' + filename)