def rotate_stream(st, **kwargs):
    """        
    The method calls the obspy.Stream.rotate method, which sometimes runs into
    errors if differences are present among the starttimes and/or endtimes of 
    the traces constituting the stream. These are prevented by slicing the 
    stream to a common time window and (if necessary) interpolating it so as to 
    avoid sub-sample differences.
    
    Parameters
    ----------
    st : obspy.Stream
    kwargs : dict
        Optional arguments passed to obspy.Stream.rotate
    
    Returns
    -------
    st : obspy.Stream
        Rotated copy of the input Stream
    """
    
    def starttime_and_endtime(st):
        starttime = max([tr.stats.starttime for tr in st])
        endtime = min([tr.stats.endtime for tr in st])
        return starttime, endtime
    
    try:
        starttime, endtime = starttime_and_endtime(st)
        st = st.slice(starttime, endtime)
        st = st.rotate(**kwargs)
    except ValueError:
        starttime, endtime = starttime_and_endtime(st)
        st.interpolate(sampling_rate=st[0].stats.sampling_rate, 
                       starttime=starttime)
        starttime, endtime = starttime_and_endtime(st)
        st = st.slice(starttime, endtime)
        st = st.rotate(**kwargs)
    return st