code: def last_kstp_from_kper(hds,kper):
      
      #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
docs: function to find the last time step (kstp) for a
      give stress period (kper) in a modflow head save file.
  
  
      Parameters
      ----------
      hds : flopy.utils.HeadFile
  
      kper : int
          the zero-index stress period number
  
      Returns
      -------
      kstp : int
          the zero-based last time step during stress period
          kper in the head save file