code: def daily_downsampler(da, freq='YS'):
      r
      if isinstance(da.time.values[0], np.datetime64):
          years = ['{:04d}'.format(y) for y in da.time.dt.year.values]
          months = ['{:02d}'.format(m) for m in da.time.dt.month.values]
      else:
          years = ['{:04d}'.format(v.year) for v in da.time.values]
          months = ['{:02d}'.format(v.month) for v in da.time.values]
      seasons = ['DJF DJF MAM MAM MAM JJA JJA JJA SON SON SON DJF'.split()[int(m) - 1] for m in months]
      n_t = da.time.size
      if freq == 'YS':
          l_tags = years
      elif freq == 'MS':
          l_tags = [years[i] + months[i] for i in range(n_t)]
      elif freq == 'QS-DEC':
          ys = []
          for i in range(n_t):
              m = months[i]
              s = seasons[i]
              y = years[i]
              if m == '12':
                  y = str(int(y) + 1)
              ys.append(y + s)
          l_tags = ys
      else:
          raise RuntimeError('freqency {:s} not implemented'.format(freq))
      buffer = da.copy()
      buffer.coords['tags'] = ('time', l_tags)
      return buffer.groupby('tags')