Separe mseed data to SDS structure

Hello again,
I would to separe mseed waves block in SDS structure.
I have a function to to that, but I encountered some errors.

This function read data from temporary file in a variable, and, with support of an other variable, I would to insert it in SDS structure.
First of all I calculate the file name and insert it in temporary variable of filename.
If file that i would write exist, I read it in support variable and, then I insert on it data reading from temporary variables. If file out is not present, I insert on this variable only temporly data.
The break of this loop is when I calculate the output file: When is different from that I have in variable control, I try to sort data and write file but now the problem is if I sort data in support variable it give me error and I do not know why. The error is “existing_trace.sort([“starttime”])
AttributeError: ‘Trace’ object has no attribute ‘sort’” and I do not know why.
Now I post the code and if anybody can help me, I will like to exlain row by row the funcion.

Thanks in adwance,


def restoreData(fileReading,destination):

  ''' Function to read file
  global logFile, events
  logFile = os.path.join(os.path.expanduser("~"),'suds_log')
  ''' Read file length
  letti = 0
  letto = 0
  Aggiunti = 0
  fileSize = os.path.getsize(fileReading)
  wave = []
  Stringa = []
  datachan = {}

  New_tr = obspy.Trace(data = np.array(values), header = {
  "network": Net,
  "station": Station,
  "location": "",
  "channel": BandCode+IntCode+Component,
  "starttime" : datetime(Anno, Mese, Giorno, Ore, Minuti, Secondi),
  "sampling_rate": Rate,
  "npts": len(values)})

  date_format = '%Y-%m-%d %H:%M:%S'
  strfatti = 0

  print('Reading data file')
  streams = read(fileReading)  
  Added = False
  LocalFile = ''
  print('Starting processing file')
  for st in streams:

    DataOnda = str(st.stats.starttime)
    DotIndex = DataOnda.find('.')

    if DotIndex != -1:
      DataOnda = DataOnda[:DotIndex]

    DataOnda = DataOnda.replace('T',' ')
    anno = str(datetime.strptime(DataOnda, date_format).timetuple().tm_year)
    giorno = ('000'+str(datetime.strptime(DataOnda, date_format).timetuple().tm_yday))[-3:]
    ultimateFile = (ultimateFile_Mask%(, st.stats.station, st.stats.location,, st.stats.mseed.dataquality, anno, float(giorno)))
    ultimateFolder = '/'.join([destination, str(anno),, st.stats.station, + '.' + st.stats.mseed.dataquality]).replace('//','/')

    if not os.path.exists(ultimateFolder):
    ultimateFolder = '/'.join([ultimateFolder, ultimateFile])
    if LocalFile != ultimateFolder:
      if Added == True:
          existing_trace.write(ultimateFolder, format = 'MSEED', reclen = 512)
          existing_trace = None 
        except Exception as e:
          with open(logFile,'at') as errore:
            errore.write(str(e) +'\n')
      LocalFile = ultimateFolder

      Added = False    
      if not os.path.isfile(ultimateFolder):
        existing_trace = st
        existing_trace = read(ultimateFolder)
    if not (str(st) in str(existing_trace.__str__())):    
      existing_trace += st
      Aggiunti +=1
      Added = True
    strfatti +=1
    print('\rElaborati %i %%  frames '%(strfatti*100/len(streams)), end='')
  events +=1
  return True