docs: def sortarai(self, datablock, s, Zdiff):
          """
          sorts data block in to first_Z, first_I, etc.
          """
  
          first_Z, first_I, zptrm_check, ptrm_check, ptrm_tail = [], [], [], [], []
          field, phi, theta = "", "", ""
          starthere = 0
          Treat_I, Treat_Z, Treat_PZ, Treat_PI, Treat_M, Treat_AC = [], [], [], [], [], []
          ISteps, ZSteps, PISteps, PZSteps, MSteps, ACSteps = [], [], [], [], [], []
          GammaChecks = []  # comparison of pTRM direction acquired and lab field
          Mkeys = ['measurement_magn_moment', 'measurement_magn_volume',
                   'measurement_magn_mass', 'measurement_magnitude']
          rec = datablock[0]
          for key in Mkeys:
              if key in list(rec.keys()) and rec[key] != "":
                  momkey = key
                  break
      # first find all the steps
          for k in range(len(datablock)):
              rec = datablock[k]
              if "treatment_temp" in list(rec.keys()) and rec["treatment_temp"] != "":
                  temp = float(rec["treatment_temp"])
                  THERMAL = True
                  MICROWAVE = False
              elif "treatment_mw_power" in list(rec.keys()) and rec["treatment_mw_power"] != "":
                  THERMAL = False
                  MICROWAVE = True
                  if "measurement_description" in list(rec.keys()):
                      MW_step = rec["measurement_description"].strip(
                          '\n').split(":")
                      for STEP in MW_step:
                          if "Number" in STEP:
                              temp = float(STEP.split("-")[-1])
  
              methcodes = []
              tmp = rec["magic_method_codes"].split(":")
              for meth in tmp:
                  methcodes.append(meth.strip())
              # for thellier-thellier
              if 'LT-T-I' in methcodes and 'LP-PI-TRM' in methcodes and 'LP-TRM' not in methcodes:
                  Treat_I.append(temp)
                  ISteps.append(k)
                  if field == "":
                      field = float(rec["treatment_dc_field"])
                  if phi == "":
                      phi = float(rec['treatment_dc_field_phi'])
                      theta = float(rec['treatment_dc_field_theta'])
  
              # for Microwave
              if 'LT-M-I' in methcodes and 'LP-PI-M' in methcodes:
                  Treat_I.append(temp)
                  ISteps.append(k)
                  if field == "":
                      field = float(rec["treatment_dc_field"])
                  if phi == "":
                      phi = float(rec['treatment_dc_field_phi'])
                      theta = float(rec['treatment_dc_field_theta'])
  
      # stick  first zero field stuff into first_Z
              if 'LT-NO' in methcodes:
                  Treat_Z.append(temp)
                  ZSteps.append(k)
              if "LT-AF-Z" in methcodes and 'treatment_ac_field' in list(rec.keys()):
                  if rec['treatment_ac_field'] != "":
                      AFD_after_NRM = True
                      # consider AFD before T-T experiment ONLY if it comes before
                      # the experiment
                      for i in range(len(first_I)):
                          # check if there was an infield step before the AFD
                          if float(first_I[i][3]) != 0:
                              AFD_after_NRM = False
                          if AFD_after_NRM:
                              AF_field = 0
                              if 'treatment_ac_field' in rec:
                                  try:
                                      AF_field = float(rec['treatment_ac_field']) * 1000
                                  except ValueError:
                                      pass
  
                              dec = float(rec["measurement_dec"])
                              inc = float(rec["measurement_inc"])
                              intensity = float(rec[momkey])
                              first_I.append([273. - AF_field, 0., 0., 0., 1])
                              first_Z.append(
                                  [273. - AF_field, dec, inc, intensity, 1])  # NRM step
              if 'LT-T-Z' in methcodes or 'LT-M-Z' in methcodes:
                  Treat_Z.append(temp)
                  ZSteps.append(k)
              if 'LT-PTRM-Z':
                  Treat_PZ.append(temp)
                  PZSteps.append(k)
              if 'LT-PTRM-I' in methcodes or 'LT-PMRM-I' in methcodes:
                  Treat_PI.append(temp)
                  PISteps.append(k)
              if 'LT-PTRM-MD' in methcodes or 'LT-PMRM-MD' in methcodes:
                  Treat_M.append(temp)
                  MSteps.append(k)
              if 'LT-PTRM-AC' in methcodes or 'LT-PMRM-AC' in methcodes:
                  Treat_AC.append(temp)
                  ACSteps.append(k)
              if 'LT-NO' in methcodes:
                  dec = float(rec["measurement_dec"])
                  inc = float(rec["measurement_inc"])
                  moment = float(rec["measurement_magn_moment"])
                  if 'LP-PI-M' not in methcodes:
                      first_I.append([273, 0., 0., 0., 1])
                      first_Z.append([273, dec, inc, moment, 1])  # NRM step
                  else:
                      first_I.append([0, 0., 0., 0., 1])
                      first_Z.append([0, dec, inc, moment, 1])  # NRM step
  
          #---------------------
          # find  IZ and ZI
          #---------------------
  
          for temp in Treat_I:  # look through infield steps and find matching Z step
              if temp in Treat_Z:  # found a match
                  istep = ISteps[Treat_I.index(temp)]
                  irec = datablock[istep]
                  methcodes = []
                  tmp = irec["magic_method_codes"].split(":")
                  for meth in tmp:
                      methcodes.append(meth.strip())
                  # take last record as baseline to subtract
                  brec = datablock[istep - 1]
                  zstep = ZSteps[Treat_Z.index(temp)]
                  zrec = datablock[zstep]
          # sort out first_Z records
                  # check if ZI/IZ in in method codes:
                  ZI = ""
                  if "LP-PI-TRM-IZ" in methcodes or "LP-PI-M-IZ" in methcodes or "LP-PI-IZ" in methcodes:
                      ZI = 0
                  elif "LP-PI-TRM-ZI" in methcodes or "LP-PI-M-ZI" in methcodes or "LP-PI-ZI" in methcodes:
                      ZI = 1
                  elif "LP-PI-BT-IZZI" in methcodes:
                      ZI == ""
                      i_intex, z_intex = 0, 0
                      foundit = False
                      for i in range(len(datablock)):
                          if THERMAL:
                              if ('treatment_temp' in list(datablock[i].keys()) and float(temp) == float(datablock[i]['treatment_temp'])):
                                  foundit = True
                          if MICROWAVE:
                              if ('measurement_description' in list(datablock[i].keys())):
                                  MW_step = datablock[i]["measurement_description"].strip(
                                      '\n').split(":")
                                  for STEP in MW_step:
                                      if "Number" in STEP:
                                          ThisStep = float(STEP.split("-")[-1])
                                          if ThisStep == float(temp):
                                              foundit = True
                          if foundit:
                              if "LT-T-Z" in datablock[i]['magic_method_codes'].split(":") or "LT-M-Z" in datablock[i]['magic_method_codes'].split(":"):
                                  z_intex = i
                              if "LT-T-I" in datablock[i]['magic_method_codes'].split(":") or "LT-M-I" in datablock[i]['magic_method_codes'].split(":"):
                                  i_intex = i
                              foundit = False
  
                      if z_intex < i_intex:
                          ZI = 1
                      else:
                          ZI = 0
                  dec = float(zrec["measurement_dec"])
                  inc = float(zrec["measurement_inc"])
                  str = float(zrec[momkey])
                  first_Z.append([temp, dec, inc, str, ZI])
          # sort out first_I records
                  idec = float(irec["measurement_dec"])
                  iinc = float(irec["measurement_inc"])
                  istr = float(irec[momkey])
                  X = pmag.dir2cart([idec, iinc, istr])
                  BL = pmag.dir2cart([dec, inc, str])
                  I = []
                  for c in range(3):
                      I.append((X[c] - BL[c]))
                  if I[2] != 0:
                      iDir = pmag.cart2dir(I)
                      if Zdiff == 0:
                          first_I.append([temp, iDir[0], iDir[1], iDir[2], ZI])
                      else:
                          first_I.append([temp, 0., 0., I[2], ZI])
  # gamma=angle([iDir[0],iDir[1]],[phi,theta])
                  else:
                      first_I.append([temp, 0., 0., 0., ZI])
  # gamma=0.0
  # put in Gamma check (infield trm versus lab field)
  # if 180.-gamma<gamma:
  # gamma=180.-gamma
  # GammaChecks.append([temp-273.,gamma])
  
          #---------------------
          # find Thellier Thellier protocol
          #---------------------
          if 'LP-PI-II'in methcodes or 'LP-PI-T-II' in methcodes or 'LP-PI-M-II' in methcodes:
              # look through infield steps and find matching Z step
              for i in range(1, len(Treat_I)):
                  if Treat_I[i] == Treat_I[i - 1]:
                      # ignore, if there are more than
                      temp = Treat_I[i]
                      irec1 = datablock[ISteps[i - 1]]
                      dec1 = float(irec1["measurement_dec"])
                      inc1 = float(irec1["measurement_inc"])
                      moment1 = float(irec1["measurement_magn_moment"])
                      if len(first_I) < 2:
                          dec_initial = dec1
                          inc_initial = inc1
                      cart1 = np.array(pmag.dir2cart([dec1, inc1, moment1]))
                      irec2 = datablock[ISteps[i]]
                      dec2 = float(irec2["measurement_dec"])
                      inc2 = float(irec2["measurement_inc"])
                      moment2 = float(irec2["measurement_magn_moment"])
                      cart2 = np.array(pmag.dir2cart([dec2, inc2, moment2]))
  
                      # check if its in the same treatment
                      if Treat_I[i] == Treat_I[i - 2] and dec2 != dec_initial and inc2 != inc_initial:
                          continue
                      if dec1 != dec2 and inc1 != inc2:
                          zerofield = (cart2 + cart1) / 2
                          infield = (cart2 - cart1) / 2
  
                          DIR_zerofield = pmag.cart2dir(zerofield)
                          DIR_infield = pmag.cart2dir(infield)
  
                          first_Z.append(
                              [temp, DIR_zerofield[0], DIR_zerofield[1], DIR_zerofield[2], 0])
                          first_I.append(
                              [temp, DIR_infield[0], DIR_infield[1], DIR_infield[2], 0])
  
          #---------------------
          # find  pTRM checks
          #---------------------
  
          for i in range(len(Treat_PI)):  # look through infield steps and find matching Z step
  
              temp = Treat_PI[i]
              k = PISteps[i]
              rec = datablock[k]
              dec = float(rec["measurement_dec"])
              inc = float(rec["measurement_inc"])
              moment = float(rec["measurement_magn_moment"])
              phi = float(rec["treatment_dc_field_phi"])
              theta = float(rec["treatment_dc_field_theta"])
              M = np.array(pmag.dir2cart([dec, inc, moment]))
  
              foundit = False
              if 'LP-PI-II' not in methcodes:
                      # Important: suport several pTRM checks in a row, but
                      # does not support pTRM checks after infield step
                  for j in range(k, 1, -1):
                      if "LT-M-I" in datablock[j]['magic_method_codes'] or "LT-T-I" in datablock[j]['magic_method_codes']:
                          after_zerofield = 0.
                          foundit = True
                          prev_rec = datablock[j]
                          zerofield_index = j
                          break
                      if float(datablock[j]['treatment_dc_field']) == 0:
                          after_zerofield = 1.
                          foundit = True
                          prev_rec = datablock[j]
                          zerofield_index = j
                          break
              else:  # Thellier-Thellier protocol
                  foundit = True
                  prev_rec = datablock[k - 1]
                  zerofield_index = k - 1
  
              if foundit:
                  prev_dec = float(prev_rec["measurement_dec"])
                  prev_inc = float(prev_rec["measurement_inc"])
                  prev_moment = float(prev_rec["measurement_magn_moment"])
                  prev_phi = float(prev_rec["treatment_dc_field_phi"])
                  prev_theta = float(prev_rec["treatment_dc_field_theta"])
                  prev_M = np.array(pmag.dir2cart(
                      [prev_dec, prev_inc, prev_moment]))
  
                  if 'LP-PI-II' not in methcodes:
                      diff_cart = M - prev_M
                      diff_dir = pmag.cart2dir(diff_cart)
                      if after_zerofield == 0:
                          ptrm_check.append(
                              [temp, diff_dir[0], diff_dir[1], diff_dir[2], zerofield_index, after_zerofield])
                      else:
                          ptrm_check.append(
                              [temp, diff_dir[0], diff_dir[1], diff_dir[2], zerofield_index, after_zerofield])
                  else:
                      # health check for T-T protocol:
                      if theta != prev_theta:
                          diff = (M - prev_M) / 2
                          diff_dir = pmag.cart2dir(diff)
                          ptrm_check.append(
                              [temp, diff_dir[0], diff_dir[1], diff_dir[2], zerofield_index, ""])
                      else:
                          print(
                              "-W- WARNING: specimen. pTRM check not in place in Thellier Thellier protocol. step please check")
  
          #---------------------
          # find Tail checks
          #---------------------
  
          for temp in Treat_M:
              # print temp
              step = MSteps[Treat_M.index(temp)]
              rec = datablock[step]
              dec = float(rec["measurement_dec"])
              inc = float(rec["measurement_inc"])
              moment = float(rec["measurement_magn_moment"])
              foundit = False
              for i in range(1, len(datablock)):
                  if 'LT-T-Z' in datablock[i]['magic_method_codes'] or 'LT-M-Z' in datablock[i]['magic_method_codes']:
                      if (THERMAL and "treatment_temp" in list(datablock[i].keys()) and float(datablock[i]["treatment_temp"]) == float(temp))\
                         or (MICROWAVE and "measurement_description" in list(datablock[i].keys()) and "Step Number-%.0f" % float(temp) in datablock[i]["measurement_description"]):
                          prev_rec = datablock[i]
                          prev_dec = float(prev_rec["measurement_dec"])
                          prev_inc = float(prev_rec["measurement_inc"])
                          prev_moment = float(
                              prev_rec["measurement_magn_moment"])
                          foundit = True
                          break
  
              if foundit:
                  ptrm_tail.append([temp, 0, 0, moment - prev_moment])
  
      #
      # final check
      #
          if len(first_Z) != len(first_I):
              print(len(first_Z), len(first_I))
              print(" Something wrong with this specimen! Better fix it or delete it ")
              input(" press return to acknowledge message")
  
          #---------------------
          # find  Additivity (patch by rshaar)
          #---------------------
  
          additivity_check = []
          for i in range(len(Treat_AC)):
              step_0 = ACSteps[i]
              temp = Treat_AC[i]
              dec0 = float(datablock[step_0]["measurement_dec"])
              inc0 = float(datablock[step_0]["measurement_inc"])
              moment0 = float(datablock[step_0]['measurement_magn_moment'])
              V0 = pmag.dir2cart([dec0, inc0, moment0])
              # find the infield step that comes before the additivity check
              foundit = False
              for j in range(step_0, 1, -1):
                  if "LT-T-I" in datablock[j]['magic_method_codes']:
                      foundit = True
                      break
              if foundit:
                  dec1 = float(datablock[j]["measurement_dec"])
                  inc1 = float(datablock[j]["measurement_inc"])
                  moment1 = float(datablock[j]['measurement_magn_moment'])
                  V1 = pmag.dir2cart([dec1, inc1, moment1])
                  # print "additivity check: ",s
                  # print j
                  # print "ACC=V1-V0:"
                  # print "V1=",[dec1,inc1,moment1],pmag.dir2cart([dec1,inc1,moment1])/float(datablock[0]["measurement_magn_moment"])
                  # print "V1=",pmag.dir2cart([dec1,inc1,moment1])/float(datablock[0]["measurement_magn_moment"])
                  # print "V0=",[dec0,inc0,moment0],pmag.dir2cart([dec0,inc0,moment0])/float(datablock[0]["measurement_magn_moment"])
                  # print "NRM=",float(datablock[0]["measurement_magn_moment"])
                  # print "-------"
  
                  I = []
                  for c in range(3):
                      I.append(V1[c] - V0[c])
                  dir1 = pmag.cart2dir(I)
                  additivity_check.append([temp, dir1[0], dir1[1], dir1[2]])
                  # print
                  # "I",np.array(I)/float(datablock[0]["measurement_magn_moment"]),dir1,"(dir1
                  # unnormalized)"
                  X = np.array(I) / \
                      float(datablock[0]["measurement_magn_moment"])
                  # print "I",np.sqrt(sum(X**2))
          araiblock = (first_Z, first_I, ptrm_check, ptrm_tail,
                       zptrm_check, GammaChecks, additivity_check)
  
          return araiblock, field