code: def merge_noises(pkllist, outroot=''):
      
  
      assert isinstance(pkllist, list), "pkllist must be list of file names"
      if not outroot:
          outroot = '_'.join(pkllist[0].split('_')[1:-1])
  
      workdir = os.path.dirname(pkllist[0])
      mergepkl = os.path.join(workdir, 'noise_' + outroot + '_merge.pkl')
  
      pkllist = [pkllist[i] for i in range(len(pkllist)) if ('merge' not in pkllist[i]) and ('seg' not in pkllist[i])]  # filter list down to per-scan noise pkls
      pkllist.sort(key=lambda i: int(i.rstrip('.pkl').split('_sc')[1]))  # sort by scan assuming filename structure
      scans = [int(ff.rstrip('.pkl').split('_sc')[1]) for ff in pkllist]
      logger.info('Aggregating noise from scans %s' % scans)
  
      allnoise = []
      for pklfile in pkllist:
          scan = int(pklfile.rstrip('.pkl').split('_sc')[1])   # parsing filename to get scan number
          with open(pklfile, 'r') as pkl:
              noises = pickle.load(pkl)   # gets all noises for segment as list
          allnoise += [[scan] + list(noise) for noise in noises]  # prepend scan number
  
      # write noise to single file
      if os.path.exists(mergepkl):
          logger.info('Overwriting merged noise file %s' % mergepkl)
          os.remove(mergepkl)
      else:
          logger.info('Writing merged noise file %s' % mergepkl)
  
      with open(mergepkl, 'w') as pkl:
          pickle.dump(allnoise, pkl, protocol=2)
docs: Merge noise files from multiple segments.
      Output noise file has scan number at start of each entry.