docs: def sortarai(self,datablock,s,Zdiff):
          """
           sorts data block in to first_Z, first_I, etc.
          """
  #        print "calling sortarai()"
          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]  # finds which type of magnetic measurement is present in magic_measurements.txt, then assigns momkey to that value
          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)):  # iterates through records.
              rec=datablock[k]
              if "treatment_temp" in list(rec.keys()):
                  temp=float(rec["treatment_temp"])
              elif "treatment_mw_power" in list(rec.keys()):
                  temp=float(rec["treatment_mw_power"])
  
              methcodes=[]
              tmp=rec["magic_method_codes"].split(":")
              for meth in tmp:
                  methcodes.append(meth.strip())
                  # methchodes contains all codes for a particular record
              # for thellier-thellier
              if 'LT-T-I' in methcodes and 'LP-PI-TRM' in methcodes and 'LP-TRM' not in methcodes :
                  # IF specimen cooling AND using a laboratory trm AND NOT trm acquisition
                  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 :
                  # if using microwave radiation in lab field AND using microwave demagnetisation
                  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:
                  # if no treatments applied before measurements
                  Treat_Z.append(temp)
                  ZSteps.append(k)
              if 'LT-T-Z' in methcodes or 'LT-M-Z' in methcodes:
                  # if specimen cooling in zero field OR using microwave radiation: In zero field
                  Treat_Z.append(temp)
                  ZSteps.append(k)
              if 'LT-PTRM-Z' : # maybe this should be in methcodes ??  note I no longer understand
                  # if pTRM tail check
                  Treat_PZ.append(temp)
                  PZSteps.append(k)
              if 'LT-PTRM-I' in methcodes or 'LT-PMRM-I' in methcodes:
                  # if pTRM check
                  Treat_PI.append(temp)
                  PISteps.append(k)
              if 'LT-PTRM-MD' in methcodes:
                  # if pTRM tail check
                  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:
                  # if no treatments applied before measurement
                  dec=float(rec["measurement_dec"])
                  inc=float(rec["measurement_inc"])
                  str=float(rec[momkey])
                  if 'LP-PI-M'  not in methcodes:
                      # if not using microwave demagnetisation
                      first_I.append([273,0.,0.,0.,1])
                      first_Z.append([273,dec,inc,str,1])  # NRM step
                  else:
                      first_I.append([0,0.,0.,0.,1])
                      first_Z.append([0,dec,inc,str,1])  # NRM step
  
                      # the block above seems to be sorting out into wheter it is Treat_Z (zero field), Treat_I (infield), a ptrm check, or a ptrm tail check. so, each record has been appended to whichever of those it belongs in.
          #---------------------
          # 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())
                  brec=datablock[istep-1] # take last record as baseline to subtract
                  zstep=ZSteps[Treat_Z.index(temp)]
                  zrec=datablock[zstep]
          # sort out first_Z records
                  if "LP-PI-TRM-IZ" in methcodes or "LP-PI-M-IZ" in methcodes:
                      ZI=0
                  else:
                      ZI=1
                  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
                  #print 'irec', irec # full data set for infield measurement
                  #print 'zrec', zrec # coresponding zerofield measurement
                  idec=float(irec["measurement_dec"])
                  iinc=float(irec["measurement_inc"])
                  istr=float(irec[momkey])
                  X=self.dir2cart([idec,iinc,istr])
                  BL=self.dir2cart([dec,inc,str])
                  I=[]
                  for c in range(3):
                      I.append((X[c]-BL[c]))
                  iDir=self.cart2dir(I)
                  first_I.append([temp,iDir[0],iDir[1],iDir[2],ZI])
                  now_ignore = """
                  #if I[2]!=0: # lj PUT THIS BACK
                  if True:
                      iDir=self.cart2dir(I)
                      if Zdiff==0:
                          print "Zdiff == 0, appending to first_I" #lj
                          print [temp,iDir[0],iDir[1],iDir[2],ZI] #lj
                          first_I.append([temp,iDir[0],iDir[1],iDir[2],ZI])
                      else:
                          print "Zdiff != 0, appending to first_I" #lj
                          print [temp,0.,0.,I[2],ZI]  #lj
                          first_I.append([temp,0.,0.,I[2],ZI])
  ##                    gamma=angle([iDir[0],iDir[1]],[phi,theta])
                  else:
                      print "0,0,0 appending to first_I"
                      print [temp,0.,0.,0.,ZI]
                      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:
              for i in range(1,len(Treat_I)): # look through infield steps and find matching Z step
                  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=numpy.array(self.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=numpy.array(self.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=old_div((cart2+cart1),2)
                          infield=old_div((cart2-cart1),2)
  
                          DIR_zerofield=self.cart2dir(zerofield)
                          DIR_infield=self.cart2dir(infield)
  
                          first_Z.append([temp,DIR_zerofield[0],DIR_zerofield[1],DIR_zerofield[2],0])
                          print("appending to first_I") # LJ remove this
                          print([temp,DIR_infield[0],DIR_infield[1],DIR_infield[2],0]) # LJ remove this
                          first_I.append([temp,DIR_infield[0],DIR_infield[1],DIR_infield[2],0])
  
  
          #---------------------
          # find  pTRM checks
          #---------------------
  
          for temp in Treat_PI: # look through infield steps and find matching Z step
              if 'LP-PI-II' not in methcodes:
                  step=PISteps[Treat_PI.index(temp)]
                  rec=datablock[step]
                  dec=float(rec["measurement_dec"])
                  inc=float(rec["measurement_inc"])
                  str=float(rec[momkey])
                  brec=datablock[step-1] # take last record as baseline to subtract
                  pdec=float(brec["measurement_dec"])
                  pinc=float(brec["measurement_inc"])
                  pint=float(brec[momkey])
                  X=self.dir2cart([dec,inc,str])
                  prevX=self.dir2cart([pdec,pinc,pint])
                  I=[]
                  for c in range(3): I.append(X[c]-prevX[c])
                  dir1=self.cart2dir(I)
                  if Zdiff==0:
                      ptrm_check.append([temp,dir1[0],dir1[1],dir1[2]])
                  else:
                      ptrm_check.append([temp,0.,0.,I[2]])
              else:
                  step=PISteps[Treat_PI.index(temp)]
                  rec=datablock[step]
                  dec=float(rec["measurement_dec"])
                  inc=float(rec["measurement_inc"])
                  moment=float(rec["measurement_magn_moment"])
                  for zerofield in first_Z:
                      if zerofield[0]==temp:
                          M1=numpy.array(self.dir2cart([dec,inc,moment]))
                          M2=numpy.array(self.dir2cart([zerofield[1],zerofield[2],zerofield[3]]))
                          diff=M1-M2
                          diff_cart=self.cart2dir(diff)
                          ptrm_check.append([temp,diff_cart[0],diff_cart[1],diff_cart[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["measurement_dec"])
              inc=float(rec["measurement_inc"])
              str=float(rec[momkey])
              brec=datablock[step-1]
              pdec=float(brec["measurement_dec"])
              pinc=float(brec["measurement_inc"])
              pint=float(brec[momkey])
              X=self.dir2cart([dec,inc,str])
              prevX=self.dir2cart([pdec,pinc,pint])
              I=[]
              for c in range(3): I.append(X[c]-prevX[c])
              dir2=self.cart2dir(I)
              zptrm_check.append([temp,dir2[0],dir2[1],dir2[2]])
          ## get pTRM tail checks together -
          for temp in Treat_M:
              step=MSteps[Treat_M.index(temp)] # tail check step - just do a difference in magnitude!
              rec=datablock[step]
              str=float(rec[momkey])
              if temp in Treat_Z:
                  step=ZSteps[Treat_Z.index(temp)]
                  brec=datablock[step]
                  pint=float(brec[momkey])
                  ptrm_tail.append([temp,0,0,str-pint])  # difference - if negative, negative tail!
              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")
  
         #---------------------
          # 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=self.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'])
                  #lj
                  start_temp=float(datablock[j]['treatment_temp']);
                  #lj
                  V1=self.dir2cart([dec1,inc1,moment1])
  
                  I=[]
                  #print "temp (K)", temp - 273
                  #print "start_temp (K)", start_temp - 273
                  #print "dec0: {}, inc0: {}, moment0: {}".format(dec0, inc0, moment0)
                  #print "V0: ", V0
                  #print "dec1: {}, inc1: {}, moment1: {}".format(dec1, inc1,moment1)
                  #print "V1: ", V1
                  #print "---"
                  for c in range(3): I.append(V1[c]-V0[c])
                  dir1=self.cart2dir(I)
                  additivity_check.append([temp,dir1[0],dir1[1],dir1[2]])
  
          araiblock=(first_Z,first_I,ptrm_check,ptrm_tail,zptrm_check,GammaChecks,additivity_check)
  
  #        print "done with sortarai()"
  #        print "araiblock[0] (first_Z) "
          #  [[273, 277.5, 79.6, 1.66e-09, 1], .....]
  #        print araiblock[0]
  #        print "araiblock[0][0]:"
  #        print araiblock[0][0]
  #        print "araiblock[1] (first_I)"
  #        print araiblock[1]
  #        print "araiblock[2] (ptrm_check)"
  #        print araiblock[2]
  #        print "araiblock[3] (ptrm_tail)"
  #        print araiblock[3]
  #        print "araiblock[4] (zptrm_check)"
  #        print araiblock[4]
  #        print "araiblock[5] (GammaChecks) "
  #        print araiblock[5]
  #        print "field ", field
          return araiblock,field