import re


def find_subroutine(lines,lineno):
   """
   Given the starting line number (lineno) and a list of lines find the 
   line numbers where the next subroutine starts and ends. Here we use
   Fortran90 coding styles so the starting and end points can be identified
   by looking for
   - subroutine
   - end subroutine
   A tuple with the corresponding starting and end line numbers is returned.
   """
   length = len(lines)
   line = lineno
   lineno_start = -1
   lineno_end   = -1
   pattern = re.compile("      subroutine")
   while not pattern.match(lines[line]):
      line += 1
      if line >= length:
         break
   if line < length:
      lineno_start = line
   # next statement needed to guarantee that line is in the valid range
   if line >= length:
      line = length-1
   pattern = re.compile("      end subroutine")
   while not pattern.match(lines[line]):
      line += 1
      if line >= length:
         break
   if line < length:
      lineno_end = line
   return (lineno_start,lineno_end)