def growing_season_length(tas, thresh='5.0 degC', window=6, freq='YS'):
    r"""Growing season length.

    The number of days between the first occurrence of at least
    six consecutive days with mean daily temperature over 5℃ and
    the first occurrence of at least six consecutive days with
    mean daily temperature below 5℃ after July 1st in the northern
    hemisphere and January 1st in the southern hemisphere.

    Parameters
    ---------
    tas : xarray.DataArray
      Mean daily temperature [℃] or [K]
    thresh : str
      Threshold temperature on which to base evaluation [℃] or [K]. Default: '5.0 degC'.
    window : int
      Minimum number of days with temperature above threshold to mark the beginning and end of growing season.
    freq : str, optional
      Resampling frequency

    Returns
    -------
    xarray.DataArray
      Growing season length.

    Notes
    -----
    Let :math:`TG_{ij}` be the mean temperature at day :math:`i` of period :math:`j`. Then counted is
    the number of days between the first occurrence of at least 6 consecutive days with:

    .. math::

        TG_{ij} > 5 ℃

    and the first occurrence after 1 July of at least 6 consecutive days with:

    .. math::

        TG_{ij} < 5 ℃
    """

    # i = xr.DataArray(np.arange(tas.time.size), dims='time')
    # ind = xr.broadcast(i, tas)[0]
    #
    # c = ((tas > thresh) * 1).rolling(time=window).sum()
    # i1 = ind.where(c == window).resample(time=freq).min(dim='time')
    #
    # # Resample sets the time to T00:00.
    # i11 = i1.reindex_like(c, method='ffill')
    #
    # # TODO: Adjust for southern hemisphere
    #
    # #i2 = ind.where(c == 0).where(tas.time.dt.month >= 7)
    # # add check to make sure indice of end of growing season is after growing season start
    # i2 = ind.where((c==0) & (ind > i11)).where(tas.time.dt.month >= 7)
    #
    # d = i2 - i11
    #
    # # take min value (first occurence after july)
    # gsl = d.resample(time=freq).min(dim='time')
    #
    # # turn nan into 0
    # gsl = xr.where(np.isnan(gsl), 0, gsl)

    # compute growth season length on resampled data
    thresh = utils.convert_units_to(thresh, tas)

    c = ((tas > thresh) * 1).rolling(time=window).sum().chunk(tas.chunks)

    def compute_gsl(c):
        nt = c.time.size
        i = xr.DataArray(np.arange(nt), dims='time').chunk({'time': 1})
        ind = xr.broadcast(i, c)[0].chunk(c.chunks)
        i1 = ind.where(c == window).min(dim='time')
        i1 = xr.where(np.isnan(i1), nt, i1)
        i11 = i1.reindex_like(c, method='ffill')
        i2 = ind.where((c == 0) & (ind > i11)).where(c.time.dt.month >= 7)
        i2 = xr.where(np.isnan(i2), nt, i2)
        d = (i2 - i1).min(dim='time')
        return d

    gsl = c.resample(time=freq).apply(compute_gsl)

    return gsl
r"""Growing season length.

    The number of days between the first occurrence of at least
    six consecutive days with mean daily temperature over 5℃ and
    the first occurrence of at least six consecutive days with
    mean daily temperature below 5℃ after July 1st in the northern
    hemisphere and January 1st in the southern hemisphere.

    Parameters
    ---------
    tas : xarray.DataArray
      Mean daily temperature [℃] or [K]
    thresh : str
      Threshold temperature on which to base evaluation [℃] or [K]. Default: '5.0 degC'.
    window : int
      Minimum number of days with temperature above threshold to mark the beginning and end of growing season.
    freq : str, optional
      Resampling frequency

    Returns
    -------
    xarray.DataArray
      Growing season length.

    Notes
    -----
    Let :math:`TG_{ij}` be the mean temperature at day :math:`i` of period :math:`j`. Then counted is
    the number of days between the first occurrence of at least 6 consecutive days with:

    .. math::

        TG_{ij} > 5 ℃

    and the first occurrence after 1 July of at least 6 consecutive days with:

    .. math::

        TG_{ij} < 5 ℃