nodes: 0, module; 1, function_definition; 2, function_name:daily_downsampler; 3, parameters; 4, block; 5, identifier:da; 6, default_parameter; 7, expression_statement; 8, comment:# generate tags from da.time and freq; 9, if_statement; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, comment:# add tags to buffer DataArray; 14, expression_statement; 15, expression_statement; 16, comment:# return groupby according to tags; 17, return_statement; 18, identifier:freq; 19, string; 20, comment:r"""Daily climate data downsampler
  
      Parameters
      ----------
      da : xarray.DataArray
      freq : string
  
      Returns
      -------
      xarray.DataArray
  
  
      Note
      ----
  
          Usage Example
  
              grouper = daily_downsampler(da_std, freq='YS')
              x2 = grouper.mean()
  
              # add time coords to x2 and change dimension tags to time
              time1 = daily_downsampler(da_std.time, freq=freq).first()
              x2.coords['time'] = ('tags', time1.values)
              x2 = x2.swap_dims({'tags': 'time'})
              x2 = x2.sortby('time')
      """; 21, call; 22, block; 23, else_clause; 24, assignment; 25, assignment; 26, comparison_operator:freq == 'YS'; 27, comment:# year start frequency; 28, block; 29, elif_clause; 30, elif_clause; 31, else_clause; 32, assignment; 33, assignment; 34, call; 35, string_content:YS; 36, identifier:isinstance; 37, argument_list; 38, expression_statement; 39, expression_statement; 40, comment:# cannot use year, month, season attributes, not available for all calendars ...; 41, block; 42, identifier:seasons; 43, list_comprehension; 44, identifier:n_t; 45, attribute; 46, identifier:freq; 47, string; 48, expression_statement; 49, comparison_operator:freq == 'MS'; 50, comment:# month start frequency; 51, block; 52, comparison_operator:freq == 'QS-DEC'; 53, comment:# DJF, MAM, JJA, SON seasons; 54, comment:# construct tags from list of season+year, increasing year for December; 55, block; 56, block; 57, identifier:buffer; 58, call; 59, subscript; 60, tuple; 61, attribute; 62, argument_list; 63, subscript; 64, attribute; 65, assignment; 66, assignment; 67, expression_statement; 68, expression_statement; 69, subscript; 70, for_in_clause; 71, attribute; 72, identifier:size; 73, string_content:YS; 74, assignment; 75, identifier:freq; 76, string; 77, expression_statement; 78, identifier:freq; 79, string; 80, expression_statement; 81, for_statement; 82, expression_statement; 83, raise_statement; 84, attribute; 85, argument_list; 86, attribute; 87, string; 88, string; 89, identifier:l_tags; 90, identifier:buffer; 91, identifier:groupby; 92, string; 93, attribute; 94, integer:0; 95, identifier:np; 96, identifier:datetime64; 97, identifier:years; 98, list_comprehension; 99, identifier:months; 100, list_comprehension; 101, assignment; 102, assignment; 103, call; 104, binary_operator:int(m) - 1; 105, identifier:m; 106, identifier:months; 107, identifier:da; 108, identifier:time; 109, identifier:l_tags; 110, identifier:years; 111, string_content:MS; 112, assignment; 113, string_content:QS-DEC; 114, assignment; 115, identifier:i; 116, call; 117, block; 118, assignment; 119, call; 120, identifier:da; 121, identifier:copy; 122, identifier:buffer; 123, identifier:coords; 124, string_content:tags; 125, string_content:time; 126, string_content:tags; 127, attribute; 128, identifier:values; 129, call; 130, for_in_clause; 131, call; 132, for_in_clause; 133, identifier:years; 134, list_comprehension; 135, identifier:months; 136, list_comprehension; 137, attribute; 138, argument_list; 139, call; 140, integer:1; 141, identifier:l_tags; 142, list_comprehension; 143, identifier:ys; 144, list; 145, identifier:range; 146, argument_list; 147, expression_statement; 148, expression_statement; 149, expression_statement; 150, if_statement; 151, expression_statement; 152, identifier:l_tags; 153, identifier:ys; 154, identifier:RuntimeError; 155, argument_list; 156, identifier:da; 157, identifier:time; 158, attribute; 159, argument_list; 160, identifier:y; 161, attribute; 162, attribute; 163, argument_list; 164, identifier:m; 165, attribute; 166, call; 167, for_in_clause; 168, call; 169, for_in_clause; 170, string; 171, identifier:split; 172, identifier:int; 173, argument_list; 174, binary_operator:years[i] + months[i]; 175, for_in_clause; 176, identifier:n_t; 177, assignment; 178, assignment; 179, assignment; 180, comparison_operator:m == '12'; 181, block; 182, call; 183, call; 184, string; 185, identifier:format; 186, identifier:y; 187, attribute; 188, identifier:values; 189, string; 190, identifier:format; 191, identifier:m; 192, attribute; 193, identifier:values; 194, attribute; 195, argument_list; 196, identifier:v; 197, attribute; 198, attribute; 199, argument_list; 200, identifier:v; 201, attribute; 202, string_content:DJF DJF MAM MAM MAM JJA JJA JJA SON SON SON DJF; 203, identifier:m; 204, subscript; 205, subscript; 206, identifier:i; 207, call; 208, identifier:m; 209, subscript; 210, identifier:s; 211, subscript; 212, identifier:y; 213, subscript; 214, identifier:m; 215, string; 216, expression_statement; 217, attribute; 218, argument_list; 219, attribute; 220, argument_list; 221, string_content:{:04d}; 222, attribute; 223, identifier:year; 224, string_content:{:02d}; 225, attribute; 226, identifier:month; 227, string; 228, identifier:format; 229, attribute; 230, attribute; 231, identifier:values; 232, string; 233, identifier:format; 234, attribute; 235, attribute; 236, identifier:values; 237, identifier:years; 238, identifier:i; 239, identifier:months; 240, identifier:i; 241, identifier:range; 242, argument_list; 243, identifier:months; 244, identifier:i; 245, identifier:seasons; 246, identifier:i; 247, identifier:years; 248, identifier:i; 249, string_content:12; 250, assignment; 251, identifier:ys; 252, identifier:append; 253, binary_operator:y + s; 254, string; 255, identifier:format; 256, identifier:freq; 257, attribute; 258, identifier:dt; 259, attribute; 260, identifier:dt; 261, string_content:{:04d}; 262, identifier:v; 263, identifier:year; 264, identifier:da; 265, identifier:time; 266, string_content:{:02d}; 267, identifier:v; 268, identifier:month; 269, identifier:da; 270, identifier:time; 271, identifier:n_t; 272, identifier:y; 273, call; 274, identifier:y; 275, identifier:s; 276, string_content:freqency {:s} not implemented; 277, identifier:da; 278, identifier:time; 279, identifier:da; 280, identifier:time; 281, identifier:str; 282, argument_list; 283, binary_operator:int(y) + 1; 284, call; 285, integer:1; 286, identifier:int; 287, argument_list; 288, identifier:y
code: def daily_downsampler(da, freq='YS'):
      r"""Daily climate data downsampler
  
      Parameters
      ----------
      da : xarray.DataArray
      freq : string
  
      Returns
      -------
      xarray.DataArray
  
  
      Note
      ----
  
          Usage Example
  
              grouper = daily_downsampler(da_std, freq='YS')
              x2 = grouper.mean()
  
              # add time coords to x2 and change dimension tags to time
              time1 = daily_downsampler(da_std.time, freq=freq).first()
              x2.coords['time'] = ('tags', time1.values)
              x2 = x2.swap_dims({'tags': 'time'})
              x2 = x2.sortby('time')
      """
  
      # generate tags from da.time and freq
      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:
          # cannot use year, month, season attributes, not available for all calendars ...
          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':
          # year start frequency
          l_tags = years
      elif freq == 'MS':
          # month start frequency
          l_tags = [years[i] + months[i] for i in range(n_t)]
      elif freq == 'QS-DEC':
          # DJF, MAM, JJA, SON seasons
          # construct tags from list of season+year, increasing year for December
          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))
  
      # add tags to buffer DataArray
      buffer = da.copy()
      buffer.coords['tags'] = ('time', l_tags)
  
      # return groupby according to tags
      return buffer.groupby('tags')