def sortmwarai(datablock, exp_type):
    """
     sorts microwave double heating data block in to first_Z, first_I, etc.
    """
    first_Z, first_I, ptrm_check, ptrm_tail, zptrm_check = [], [], [], [], []
    field, phi, theta = "", "", ""
    POWT_I, POWT_Z, POWT_PZ, POWT_PI, POWT_M = [], [], [], [], []
    ISteps, ZSteps, PZSteps, PISteps, MSteps = [], [], [], [], []
    rad = old_div(np.pi, 180.)
    ThetaChecks = []
    DeltaChecks = []
    GammaChecks = []
# first find all the steps
    for k in range(len(datablock)):
        rec = datablock[k]
        powt = int(float(rec["treatment_mw_energy"]))
        methcodes = []
        tmp = rec["magic_method_codes"].split(":")
        for meth in tmp:
            methcodes.append(meth.strip())
        if 'LT-M-I' in methcodes and 'LP-MRM' not in methcodes:
            POWT_I.append(powt)
            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'])
        if 'LT-M-Z' in methcodes:
            POWT_Z.append(powt)
            ZSteps.append(k)
        if 'LT-PMRM-Z' in methcodes:
            POWT_PZ.append(powt)
            PZSteps.append(k)
        if 'LT-PMRM-I' in methcodes:
            POWT_PI.append(powt)
            PISteps.append(k)
        if 'LT-PMRM-MD' in methcodes:
            POWT_M.append(powt)
            MSteps.append(k)
        if 'LT-NO' in methcodes:
            dec = float(rec["measurement_dec"])
            inc = float(rec["measurement_inc"])
            st = float(rec["measurement_magn_moment"])
            first_I.append([0, 0., 0., 0., 1])
            first_Z.append([0, dec, inc, st, 1])  # NRM step
    if exp_type == "LP-PI-M-D":
        # now look trough infield steps and  find matching Z step
        for powt in POWT_I:
            if powt in POWT_Z:
                istep = ISteps[POWT_I.index(powt)]
                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[POWT_Z.index(powt)]
                zrec = datablock[zstep]
    # sort out first_Z records
                if "LP-PI-M-IZ" in methcodes:
                    ZI = 0
                else:
                    ZI = 1
                dec = float(zrec["measurement_dec"])
                inc = float(zrec["measurement_inc"])
                st = float(zrec["measurement_magn_moment"])
                first_Z.append([powt, dec, inc, st, ZI])
    # sort out first_I records
                idec = float(irec["measurement_dec"])
                iinc = float(irec["measurement_inc"])
                istr = float(irec["measurement_magn_moment"])
                X = dir2cart([idec, iinc, istr])
                BL = dir2cart([dec, inc, st])
                I = []
                for c in range(3):
                    I.append((X[c] - BL[c]))
                iDir = cart2dir(I)
                first_I.append([powt, iDir[0], iDir[1], iDir[2], ZI])
# put in Gamma check (infield trm versus lab field)
                gamma = angle([iDir[0], iDir[1]], [phi, theta])
                GammaChecks.append([powt, gamma])
    elif exp_type == "LP-PI-M-S":
        # find last zero field step before first infield step
        lzrec = datablock[ISteps[0] - 1]
        irec = datablock[ISteps[0]]
        ndec = float(lzrec["measurement_dec"])
        ninc = float(lzrec["measurement_inc"])
        nstr = float(lzrec["measurement_magn_moment"])
        NRM = dir2cart([ndec, ninc, nstr])
        fdec = float(irec["treatment_dc_field_phi"])
        finc = float(irec["treatment_dc_field_theta"])
        Flab = dir2cart([fdec, finc, 1.])
        for step in ISteps:
            irec = datablock[step]
            rdec = float(irec["measurement_dec"])
            rinc = float(irec["measurement_inc"])
            rstr = float(irec["measurement_magn_moment"])
            theta1 = angle([ndec, ninc], [rdec, rinc])
            theta2 = angle([rdec, rinc], [fdec, finc])
            powt = int(float(irec["treatment_mw_energy"]))
            ThetaChecks.append([powt, theta1 + theta2])
            p = (180. - (theta1 + theta2))
            nstr = rstr * (old_div(np.sin(theta2 * rad), np.sin(p * rad)))
            tmstr = rstr * (old_div(np.sin(theta1 * rad), np.sin(p * rad)))
            first_Z.append([powt, ndec, ninc, nstr, 1])
            first_I.append([powt, dec, inc, tmstr, 1])
# check if zero field steps are parallel to assumed NRM
        for step in ZSteps:
            zrec = datablock[step]
            powt = int(float(zrec["treatment_mw_energy"]))
            zdec = float(zrec["measurement_dec"])
            zinc = float(zrec["measurement_inc"])
            delta = angle([ndec, ninc], [zdec, zinc])
            DeltaChecks.append([powt, delta])
    # get pTRMs together - take previous record and subtract
    for powt in POWT_PI:
        step = PISteps[POWT_PI.index(powt)]
        rec = datablock[step]
        dec = float(rec["measurement_dec"])
        inc = float(rec["measurement_inc"])
        st = float(rec["measurement_magn_moment"])
        brec = datablock[step - 1]  # take last record as baseline to subtract
        pdec = float(brec["measurement_dec"])
        pinc = float(brec["measurement_inc"])
        pint = float(brec["measurement_magn_moment"])
        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)
        ptrm_check.append([powt, dir1[0], dir1[1], dir1[2]])
    # get zero field pTRM  checks together
    for powt in POWT_PZ:
        step = PZSteps[POWT_PZ.index(powt)]
        rec = datablock[step]
        dec = float(rec["measurement_dec"])
        inc = float(rec["measurement_inc"])
        st = float(rec["measurement_magn_moment"])
        brec = datablock[step - 1]
        pdec = float(brec["measurement_dec"])
        pinc = float(brec["measurement_inc"])
        pint = float(brec["measurement_magn_moment"])
        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([powt, dir2[0], dir2[1], dir2[2]])
    # get pTRM tail checks together -
    for powt in POWT_M:
        step = MSteps[POWT_M.index(powt)]  # tail check step
        rec = datablock[step]
#        dec=float(rec["measurement_dec"])
#        inc=float(rec["measurement_inc"])
        st = float(rec["measurement_magn_moment"])
        step = ZSteps[POWT_Z.index(powt)]
        brec = datablock[step]
#        pdec=float(brec["measurement_dec"])
#        pinc=float(brec["measurement_inc"])
        pint = float(brec["measurement_magn_moment"])
#        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([powt,d[0],d[1],d[2]])
        # just do absolute magnitude difference # not vector diff
        ptrm_tail.append([powt, 0, 0, st - pint])
    #  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")
            print(MaxRec)
    araiblock = (first_Z, first_I, ptrm_check, ptrm_tail,
                 zptrm_check, GammaChecks, ThetaChecks, DeltaChecks)
    return araiblock, field