nodes: 0, module; 1, function_definition; 2, function_name:msregularize; 3, parameters; 4, block; 5, identifier:msname; 6, identifier:newname; 7, expression_statement; 8, comment:# Find out all baselines.; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, comment:# Now iterate in time,band over the MS.; 13, for_statement; 14, comment:# Combine the existing table and new table.; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, comment:""" Regularize an MS
  
      The output MS will be such that it has the same number of baselines
      for each time stamp. Where needed fully flagged rows are added.
  
      Possibly missing rows are written into a separate MS <newname>-add.
      It is concatenated with the original MS and sorted in order of TIME,
      DATADESC_ID, ANTENNA1,ANTENNA2 to form a new regular MS. Note that
      the new MS references the input MS (it does not copy the data).
      It means that changes made in the new MS are also made in the input MS.
  
      If no rows were missing, the new MS is still created referencing the
      input MS.
      """; 20, assignment; 21, assignment; 22, assignment; 23, identifier:tsub; 24, call; 25, block; 26, comparison_operator:nadded > 0; 27, comment:# First initialize data and flags in the added rows.; 28, block; 29, else_clause; 30, call; 31, call; 32, comparison_operator:nadded > 0; 33, block; 34, else_clause; 35, identifier:t; 36, call; 37, identifier:t1; 38, call; 39, identifier:nadded; 40, integer:0; 41, attribute; 42, argument_list; 43, expression_statement; 44, if_statement; 45, if_statement; 46, identifier:nadded; 47, integer:0; 48, expression_statement; 49, expression_statement; 50, expression_statement; 51, expression_statement; 52, expression_statement; 53, block; 54, attribute; 55, argument_list; 56, attribute; 57, argument_list; 58, identifier:nadded; 59, integer:0; 60, expression_statement; 61, block; 62, identifier:table; 63, argument_list; 64, attribute; 65, argument_list; 66, identifier:t; 67, identifier:iter; 68, list; 69, assignment; 70, comparison_operator:nmissing < 0; 71, block; 72, comparison_operator:nmissing > 0; 73, comment:# Rows needs to be added for the missing baselines.; 74, block; 75, call; 76, call; 77, assignment; 78, call; 79, assignment; 80, expression_statement; 81, identifier:tcombs; 82, identifier:rename; 83, identifier:newname; 84, identifier:six; 85, identifier:print_; 86, identifier:newname; 87, string; 88, call; 89, expression_statement; 90, identifier:msname; 91, identifier:t; 92, identifier:sort; 93, string; 94, string; 95, string; 96, identifier:nmissing; 97, binary_operator:t1.nrows() - tsub.nrows(); 98, identifier:nmissing; 99, integer:0; 100, raise_statement; 101, identifier:nmissing; 102, integer:0; 103, expression_statement; 104, expression_statement; 105, expression_statement; 106, expression_statement; 107, expression_statement; 108, expression_statement; 109, if_statement; 110, comment:# If nothing added yet, create a new table.; 111, comment:# (which has to be reopened for read/write).; 112, comment:# Otherwise append to that new table.; 113, if_statement; 114, comment:# Set the correct time and band in the new rows.; 115, expression_statement; 116, expression_statement; 117, expression_statement; 118, identifier:taql; 119, argument_list; 120, identifier:taql; 121, argument_list; 122, identifier:tcomb; 123, call; 124, attribute; 125, argument_list; 126, identifier:tcombs; 127, call; 128, assignment; 129, string_content:has been created; it references the original MS; 130, attribute; 131, argument_list; 132, call; 133, string_content:unique ANTENNA1,ANTENNA2; 134, string_content:TIME; 135, string_content:DATA_DESC_ID; 136, call; 137, call; 138, call; 139, assignment; 140, assignment; 141, assignment; 142, assignment; 143, assignment; 144, call; 145, comparison_operator:t2.nrows() != nmissing; 146, block; 147, comparison_operator:nadded == 0; 148, block; 149, else_clause; 150, call; 151, call; 152, augmented_assignment; 153, string; 154, string; 155, identifier:table; 156, argument_list; 157, identifier:tcomb; 158, identifier:rename; 159, binary_operator:newname + '_adds'; 160, attribute; 161, argument_list; 162, identifier:tcombs; 163, call; 164, identifier:six; 165, identifier:print_; 166, string; 167, binary_operator:newname + '_adds'; 168, string; 169, identifier:nadded; 170, string; 171, attribute; 172, argument_list; 173, attribute; 174, argument_list; 175, attribute; 176, argument_list; 177, identifier:ValueError; 178, argument_list; 179, identifier:ant1; 180, call; 181, identifier:ant2; 182, call; 183, identifier:ant1; 184, call; 185, identifier:ant2; 186, call; 187, identifier:t2; 188, call; 189, attribute; 190, argument_list; 191, call; 192, identifier:nmissing; 193, raise_statement; 194, identifier:nadded; 195, integer:0; 196, expression_statement; 197, expression_statement; 198, block; 199, attribute; 200, argument_list; 201, attribute; 202, argument_list; 203, identifier:nadded; 204, identifier:nmissing; 205, string_content:update $tnew set DATA=0+0i; 206, string_content:update $tnew set FLAG=True; 207, list; 208, identifier:newname; 209, string; 210, identifier:tcomb; 211, identifier:sort; 212, string; 213, attribute; 214, argument_list; 215, string_content:and; 216, identifier:newname; 217, string; 218, string_content:containing; 219, string_content:new rows; 220, identifier:six; 221, identifier:print_; 222, string; 223, identifier:t1; 224, identifier:nrows; 225, identifier:tsub; 226, identifier:nrows; 227, string:"A time/band chunk has too many rows"; 228, attribute; 229, argument_list; 230, attribute; 231, argument_list; 232, attribute; 233, argument_list; 234, attribute; 235, argument_list; 236, identifier:taql; 237, argument_list; 238, identifier:six; 239, identifier:print_; 240, identifier:nmissing; 241, call; 242, call; 243, call; 244, attribute; 245, argument_list; 246, call; 247, assignment; 248, assignment; 249, expression_statement; 250, identifier:tnew; 251, identifier:putcell; 252, string; 253, call; 254, call; 255, identifier:tnew; 256, identifier:putcell; 257, string; 258, call; 259, call; 260, identifier:t; 261, identifier:tnew; 262, string_content:_adds; 263, string_content:TIME,DATA_DESC_ID,ANTENNA1,ANTENNA2; 264, identifier:t; 265, identifier:query; 266, keyword_argument; 267, string_content:_adds; 268, string_content:no rows needed to be added; 269, call; 270, identifier:replace; 271, string; 272, string; 273, call; 274, identifier:replace; 275, string; 276, string; 277, identifier:tsub; 278, identifier:getcol; 279, string; 280, identifier:tsub; 281, identifier:getcol; 282, string; 283, binary_operator:'select from $t1 where !any(ANTENNA1 == $ant1 &&' +
                        ' ANTENNA2 == $ant2)'; 284, attribute; 285, argument_list; 286, attribute; 287, argument_list; 288, attribute; 289, argument_list; 290, identifier:t2; 291, identifier:nrows; 292, identifier:ValueError; 293, argument_list; 294, identifier:tnew; 295, call; 296, identifier:tnew; 297, call; 298, call; 299, string_content:TIME; 300, identifier:range; 301, argument_list; 302, attribute; 303, argument_list; 304, string_content:DATA_DESC_ID; 305, identifier:range; 306, argument_list; 307, attribute; 308, argument_list; 309, identifier:offset; 310, integer:0; 311, identifier:str; 312, argument_list; 313, string_content:; 314, string_content:,; 315, identifier:str; 316, argument_list; 317, string_content:; 318, string_content:,; 319, string_content:ANTENNA1; 320, string_content:ANTENNA2; 321, string; 322, string; 323, identifier:t1; 324, identifier:nrows; 325, identifier:tsub; 326, identifier:nrows; 327, identifier:t2; 328, identifier:nrows; 329, string:"A time/band chunk behaves strangely"; 330, attribute; 331, argument_list; 332, identifier:table; 333, argument_list; 334, attribute; 335, argument_list; 336, identifier:nadded; 337, binary_operator:nadded + nmissing; 338, identifier:tsub; 339, identifier:getcell; 340, string; 341, integer:0; 342, identifier:nadded; 343, binary_operator:nadded + nmissing; 344, identifier:tsub; 345, identifier:getcell; 346, string; 347, integer:0; 348, call; 349, call; 350, string_content:select from $t1 where !any(ANTENNA1 == $ant1 &&; 351, string_content:ANTENNA2 == $ant2); 352, identifier:t2; 353, identifier:copy; 354, binary_operator:newname + "_add"; 355, keyword_argument; 356, binary_operator:newname + "_add"; 357, keyword_argument; 358, identifier:t2; 359, identifier:copyrows; 360, identifier:tnew; 361, identifier:nadded; 362, identifier:nmissing; 363, string_content:TIME; 364, identifier:nadded; 365, identifier:nmissing; 366, string_content:DATA_DESC_ID; 367, attribute; 368, argument_list; 369, attribute; 370, argument_list; 371, identifier:newname; 372, string:"_add"; 373, identifier:deep; 374, True; 375, identifier:newname; 376, string:"_add"; 377, identifier:readonly; 378, False; 379, identifier:t1; 380, identifier:getcol; 381, string; 382, identifier:t1; 383, identifier:getcol; 384, string; 385, string_content:ANTENNA1; 386, string_content:ANTENNA2
code: def msregularize(msname, newname):
      """ Regularize an MS
  
      The output MS will be such that it has the same number of baselines
      for each time stamp. Where needed fully flagged rows are added.
  
      Possibly missing rows are written into a separate MS <newname>-add.
      It is concatenated with the original MS and sorted in order of TIME,
      DATADESC_ID, ANTENNA1,ANTENNA2 to form a new regular MS. Note that
      the new MS references the input MS (it does not copy the data).
      It means that changes made in the new MS are also made in the input MS.
  
      If no rows were missing, the new MS is still created referencing the
      input MS.
      """
  
      # Find out all baselines.
      t = table(msname)
      t1 = t.sort('unique ANTENNA1,ANTENNA2')
      nadded = 0
      # Now iterate in time,band over the MS.
      for tsub in t.iter(['TIME', 'DATA_DESC_ID']):
          nmissing = t1.nrows() - tsub.nrows()
          if nmissing < 0:
              raise ValueError("A time/band chunk has too many rows")
          if nmissing > 0:
              # Rows needs to be added for the missing baselines.
              ant1 = str(t1.getcol('ANTENNA1')).replace(' ', ',')
              ant2 = str(t1.getcol('ANTENNA2')).replace(' ', ',')
              ant1 = tsub.getcol('ANTENNA1')
              ant2 = tsub.getcol('ANTENNA2')
              t2 = taql('select from $t1 where !any(ANTENNA1 == $ant1 &&' +
                        ' ANTENNA2 == $ant2)')
              six.print_(nmissing, t1.nrows(), tsub.nrows(), t2.nrows())
              if t2.nrows() != nmissing:
                  raise ValueError("A time/band chunk behaves strangely")
              # If nothing added yet, create a new table.
              # (which has to be reopened for read/write).
              # Otherwise append to that new table.
              if nadded == 0:
                  tnew = t2.copy(newname + "_add", deep=True)
                  tnew = table(newname + "_add", readonly=False)
              else:
                  t2.copyrows(tnew)
              # Set the correct time and band in the new rows.
              tnew.putcell('TIME',
                           range(nadded, nadded + nmissing),
                           tsub.getcell('TIME', 0))
              tnew.putcell('DATA_DESC_ID',
                           range(nadded, nadded + nmissing),
                           tsub.getcell('DATA_DESC_ID', 0))
              nadded += nmissing
      # Combine the existing table and new table.
      if nadded > 0:
          # First initialize data and flags in the added rows.
          taql('update $tnew set DATA=0+0i')
          taql('update $tnew set FLAG=True')
          tcomb = table([t, tnew])
          tcomb.rename(newname + '_adds')
          tcombs = tcomb.sort('TIME,DATA_DESC_ID,ANTENNA1,ANTENNA2')
      else:
          tcombs = t.query(offset=0)
      tcombs.rename(newname)
      six.print_(newname, 'has been created; it references the original MS')
      if nadded > 0:
          six.print_(' and', newname + '_adds', 'containing', nadded, 'new rows')
      else:
          six.print_(' no rows needed to be added')