nodes: 0, module; 1, function_definition; 2, function_name:collapse; 3, parameters; 4, block; 5, identifier:l; 6, identifier:raw; 7, expression_statement; 8, expression_statement; 9, comment:# LiPD data (in progress), sorted dataset name; 10, expression_statement; 11, expression_statement; 12, try_statement; 13, expression_statement; 14, return_statement; 15, comment:"""
      LiPD Version 1.3
      Main function to initiate time series to LiPD conversion
  
      Each object has a:
      "paleoNumber" or "chronNumber"
      "tableNumber"
      "modelNumber"
      "time_id"
      "mode" - chronData or paleoData
      "tableType" - "meas" "ens" "summ"
  
      :param list l: Time series
      :return dict _master: LiPD data, sorted by dataset name
      """; 16, call; 17, assignment; 18, assignment; 19, comment:# Determine if we're collapsing a paleo or chron time series; 20, block; 21, except_clause; 22, call; 23, identifier:_master; 24, attribute; 25, argument_list; 26, identifier:_master; 27, dictionary; 28, identifier:_dsn; 29, string:""; 30, expression_statement; 31, comment:# Loop the time series; 32, for_statement; 33, comment:# The result combined into a single dataset. Remove the extra layer on the data.; 34, if_statement; 35, as_pattern; 36, block; 37, attribute; 38, argument_list; 39, identifier:logger_ts; 40, identifier:info; 41, string:"enter collapse"; 42, assignment; 43, identifier:entry; 44, identifier:l; 45, comment:# Get notable keys; 46, block; 47, comparison_operator:len(_master) == 1; 48, block; 49, else_clause; 50, identifier:Exception; 51, as_pattern_target; 52, expression_statement; 53, expression_statement; 54, identifier:logger_ts; 55, identifier:info; 56, string:"exit collapse"; 57, identifier:_pc; 58, subscript; 59, expression_statement; 60, expression_statement; 61, expression_statement; 62, comment:# Since root items are the same in each column of the same dataset, we only need these steps the first time.; 63, if_statement; 64, comment:# Collapse pc, calibration, and interpretation; 65, expression_statement; 66, call; 67, integer:1; 68, expression_statement; 69, expression_statement; 70, block; 71, identifier:e; 72, call; 73, call; 74, subscript; 75, string:"mode"; 76, assignment; 77, assignment; 78, assignment; 79, comparison_operator:dsn not in _master; 80, block; 81, assignment; 82, identifier:len; 83, argument_list; 84, assignment; 85, call; 86, expression_statement; 87, identifier:print; 88, argument_list; 89, attribute; 90, argument_list; 91, identifier:l; 92, integer:0; 93, identifier:dsn; 94, subscript; 95, identifier:_dsn; 96, identifier:dsn; 97, identifier:_current; 98, identifier:entry; 99, identifier:dsn; 100, identifier:_master; 101, expression_statement; 102, expression_statement; 103, expression_statement; 104, try_statement; 105, identifier:_master; 106, call; 107, identifier:_master; 108, identifier:_master; 109, subscript; 110, identifier:print; 111, argument_list; 112, call; 113, call; 114, identifier:logger_ts; 115, identifier:error; 116, call; 117, identifier:entry; 118, string; 119, call; 120, call; 121, assignment; 122, block; 123, except_clause; 124, identifier:_collapse_pc; 125, argument_list; 126, identifier:_master; 127, identifier:_dsn; 128, string:"Created LiPD data: 1 dataset"; 129, identifier:print; 130, argument_list; 131, attribute; 132, argument_list; 133, attribute; 134, argument_list; 135, string_content:dataSetName; 136, attribute; 137, argument_list; 138, identifier:print; 139, argument_list; 140, pattern_list; 141, call; 142, expression_statement; 143, if_statement; 144, as_pattern; 145, block; 146, identifier:_master; 147, identifier:_current; 148, identifier:dsn; 149, identifier:_pc; 150, call; 151, string:"Error: Unable to collapse time series, {}"; 152, identifier:format; 153, identifier:e; 154, string:"collapse: Exception: {}"; 155, identifier:format; 156, identifier:e; 157, identifier:logger_ts; 158, identifier:info; 159, call; 160, call; 161, identifier:_master; 162, identifier:_current; 163, identifier:_collapse_root; 164, argument_list; 165, assignment; 166, comparison_operator:"chronData" in raw[dsn]; 167, block; 168, identifier:KeyError; 169, as_pattern_target; 170, expression_statement; 171, attribute; 172, argument_list; 173, attribute; 174, argument_list; 175, attribute; 176, argument_list; 177, identifier:_master; 178, identifier:_current; 179, identifier:dsn; 180, identifier:_pc; 181, subscript; 182, subscript; 183, string:"chronData"; 184, subscript; 185, expression_statement; 186, identifier:e; 187, call; 188, string:"Created LiPD data: {} datasets"; 189, identifier:format; 190, call; 191, string:"collapsing: {}"; 192, identifier:format; 193, identifier:dsn; 194, string:"collapsing: {}"; 195, identifier:format; 196, identifier:dsn; 197, subscript; 198, string:"paleoData"; 199, subscript; 200, string:"paleoData"; 201, identifier:raw; 202, identifier:dsn; 203, assignment; 204, identifier:print; 205, argument_list; 206, identifier:len; 207, argument_list; 208, identifier:_master; 209, identifier:dsn; 210, identifier:raw; 211, identifier:dsn; 212, subscript; 213, subscript; 214, call; 215, identifier:_master; 216, subscript; 217, string:"chronData"; 218, subscript; 219, string:"chronData"; 220, attribute; 221, argument_list; 222, identifier:_master; 223, identifier:dsn; 224, identifier:raw; 225, identifier:dsn; 226, string:"collapse: Could not collapse an object the dataset: {}, {}"; 227, identifier:format; 228, identifier:dsn; 229, identifier:e
code: def collapse(l, raw):
      """
      LiPD Version 1.3
      Main function to initiate time series to LiPD conversion
  
      Each object has a:
      "paleoNumber" or "chronNumber"
      "tableNumber"
      "modelNumber"
      "time_id"
      "mode" - chronData or paleoData
      "tableType" - "meas" "ens" "summ"
  
      :param list l: Time series
      :return dict _master: LiPD data, sorted by dataset name
      """
      logger_ts.info("enter collapse")
      # LiPD data (in progress), sorted dataset name
      _master = {}
      _dsn = ""
  
      try:
          # Determine if we're collapsing a paleo or chron time series
          _pc = l[0]["mode"]
  
          # Loop the time series
          for entry in l:
              # Get notable keys
              dsn = entry['dataSetName']
              _dsn = dsn
              _current = entry
  
              # Since root items are the same in each column of the same dataset, we only need these steps the first time.
              if dsn not in _master:
                  logger_ts.info("collapsing: {}".format(dsn))
                  print("collapsing: {}".format(dsn))
                  _master, _current = _collapse_root(_master, _current, dsn, _pc)
                  try:
                      _master[dsn]["paleoData"] = raw[dsn]["paleoData"]
                      if "chronData" in raw[dsn]:
                          _master[dsn]["chronData"] = raw[dsn]["chronData"]
                  except KeyError as e:
                      print("collapse: Could not collapse an object the dataset: {}, {}".format(dsn, e))
  
              # Collapse pc, calibration, and interpretation
              _master = _collapse_pc(_master, _current, dsn, _pc)
  
          # The result combined into a single dataset. Remove the extra layer on the data.
          if len(_master) == 1:
              _master = _master[_dsn]
              print("Created LiPD data: 1 dataset")
          else:
              print("Created LiPD data: {} datasets".format(len(_master)))
  
      except Exception as e:
          print("Error: Unable to collapse time series, {}".format(e))
          logger_ts.error("collapse: Exception: {}".format(e))
  
      logger_ts.info("exit collapse")
      return _master