def last_kstp_from_kper(hds,kper):
    """ function to find the last time step (kstp) for a
    give stress period (kper) in a modflow head save file.

    Args:
        hds (`flopy.utils.HeadFile`): head save file

        kper (`int`): the zero-index stress period number

    Returns:
        **int**: the zero-based last time step during stress period
        kper in the head save file

    """
    #find the last kstp with this kper
    kstp = -1
    for kkstp,kkper in hds.kstpkper:
        if kkper == kper+1 and kkstp > kstp:
            kstp = kkstp
    if kstp == -1:
        raise Exception("kstp not found for kper {0}".format(kper))
    kstp -= 1
    return kstp