docs: def sortarai(datablock, s, Zdiff, **kwargs):
      """
       sorts data block in to first_Z, first_I, etc.
  
      Parameters
      _________
      datablock : Pandas DataFrame with Thellier-Tellier type data
      s : specimen name
      Zdiff : if True, take difference in Z values instead of vector difference
              NB:  this should always be False
      **kwargs :
          version : data model.  if not 3, assume data model = 2.5
  
      Returns
      _______
      araiblock : [first_Z, first_I, ptrm_check,
                   ptrm_tail, zptrm_check, GammaChecks]
      field : lab field (in tesla)
      """
      if 'version' in list(kwargs.keys()) and kwargs['version'] == 3:
          dec_key, inc_key = 'dir_dec', 'dir_inc'
          Mkeys = ['magn_moment', 'magn_volume', 'magn_mass', 'magnitude']
          meth_key = 'method_codes'
          temp_key, dc_key = 'treat_temp', 'treat_dc_field'
          dc_theta_key, dc_phi_key = 'treat_dc_field_theta', 'treat_dc_field_phi'
          # convert dataframe to list of dictionaries
          datablock = datablock.to_dict('records')
      else:
          dec_key, inc_key = 'measurement_dec', 'measurement_inc'
          Mkeys = ['measurement_magn_moment', 'measurement_magn_volume',
                   'measurement_magn_mass', 'measurement_magnitude']
          meth_key = 'magic_method_codes'
          temp_key, dc_key = 'treatment_temp', 'treatment_dc_field'
          dc_theta_key, dc_phi_key = 'treatment_dc_field_theta', 'treatment_dc_field_phi'
      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 = [], [], [], [], []
      ISteps, ZSteps, PISteps, PZSteps, MSteps = [], [], [], [], []
      GammaChecks = []  # comparison of pTRM direction acquired and lab field
      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]
          temp = float(rec[temp_key])
          methcodes = []
          tmp = rec[meth_key].split(":")
          for meth in tmp:
              methcodes.append(meth.strip())
          if 'LT-T-I' in methcodes and 'LP-TRM' not in methcodes and 'LP-PI-TRM' in methcodes:
              Treat_I.append(temp)
              ISteps.append(k)
              if field == "":
                  field = float(rec[dc_key])
              if phi == "":
                  phi = float(rec[dc_phi_key])
                  theta = float(rec[dc_theta_key])
  # stick  first zero field stuff into first_Z
          if 'LT-NO' in methcodes:
              Treat_Z.append(temp)
              ZSteps.append(k)
          if 'LT-T-Z' in methcodes:
              Treat_Z.append(temp)
              ZSteps.append(k)
          if 'LT-PTRM-Z' in methcodes:
              Treat_PZ.append(temp)
              PZSteps.append(k)
          if 'LT-PTRM-I' in methcodes:
              Treat_PI.append(temp)
              PISteps.append(k)
          if 'LT-PTRM-MD' in methcodes:
              Treat_M.append(temp)
              MSteps.append(k)
          if 'LT-NO' in methcodes:
              dec = float(rec[dec_key])
              inc = float(rec[inc_key])
              st = float(rec[momkey])
              first_I.append([273, 0., 0., 0., 1])
              first_Z.append([273, dec, inc, st, 1])  # NRM step
      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[meth_key].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
              if "LP-PI-TRM-IZ" in methcodes:
                  ZI = 0
              else:
                  ZI = 1
              dec = float(zrec[dec_key])
              inc = float(zrec[inc_key])
              st = float(zrec[momkey])
              first_Z.append([temp, dec, inc, st, ZI])
      # sort out first_I records
              try:
                  idec = float(irec[dec_key])
                  iinc = float(irec[inc_key])
                  istr = float(irec[momkey])
              except TypeError as ex:
                  raise Exception('Malformed data of some sort for dec/inc/moment in measurement: {}.  You must fix this before proceeding.\n           Bad record: {}'.format(irec.get('measurement', ''), irec))
              X = dir2cart([idec, iinc, istr])
              BL = dir2cart([dec, inc, st])
              I = []
              for c in range(3):
                  I.append((X[c] - BL[c]))
              if I[2] != 0:
                  iDir = 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])
      for temp in Treat_PI:  # look through infield steps and find matching Z step
          step = PISteps[Treat_PI.index(temp)]
          rec = datablock[step]
          dec = float(rec[dec_key])
          inc = float(rec[inc_key])
          st = float(rec[momkey])
          brec = datablock[step - 1]  # take last record as baseline to subtract
          pdec = float(brec[dec_key])
          pinc = float(brec[inc_key])
          pint = float(brec[momkey])
          X = dir2cart([dec, inc, st])
          prevX = dir2cart([pdec, pinc, pint])
          I = []
          for c in range(3):
              I.append(X[c] - prevX[c])
          dir1 = cart2dir(I)
          if Zdiff == 0:
              ptrm_check.append([temp, dir1[0], dir1[1], dir1[2]])
          else:
              ptrm_check.append([temp, 0., 0., I[2]])
  # in case there are zero-field pTRM checks (not the SIO way)
      for temp in Treat_PZ:
          step = PZSteps[Treat_PZ.index(temp)]
          rec = datablock[step]
          dec = float(rec[dec_key])
          inc = float(rec[inc_key])
          st = float(rec[momkey])
          brec = datablock[step - 1]
          pdec = float(brec[dec_key])
          pinc = float(brec[inc_key])
          pint = float(brec[momkey])
          X = dir2cart([dec, inc, st])
          prevX = dir2cart([pdec, pinc, pint])
          I = []
          for c in range(3):
              I.append(X[c] - prevX[c])
          dir2 = cart2dir(I)
          zptrm_check.append([temp, dir2[0], dir2[1], dir2[2]])
      # get pTRM tail checks together -
      for temp in Treat_M:
          # tail check step - just do a difference in magnitude!
          step = MSteps[Treat_M.index(temp)]
          rec = datablock[step]
          st = float(rec[momkey])
          if temp in Treat_Z:
              step = ZSteps[Treat_Z.index(temp)]
              brec = datablock[step]
              pint = float(brec[momkey])
  #        X=dir2cart([dec,inc,st])
  #        prevX=dir2cart([pdec,pinc,pint])
  #        I=[]
  #        for c in range(3):I.append(X[c]-prevX[c])
  #        d=cart2dir(I)
  #        ptrm_tail.append([temp,d[0],d[1],d[2]])
              # difference - if negative, negative tail!
              ptrm_tail.append([temp, 0, 0, st - pint])
          else:
              print(
                  s, '  has a tail check with no first zero field step - check input file! for step', temp - 273.)
  #
  # 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")
      araiblock = (first_Z, first_I, ptrm_check,
                   ptrm_tail, zptrm_check, GammaChecks)
      return araiblock, field