def weightedkappaextension(variables, wttype=1, cilevel=95):

    varnames = expandvarnames(variables)
    caption = varnames[0] + _(" vs. ") + varnames[1]
    vardict = spssaux.VariableDict(varnames)
    if len(vardict) != len(varnames):
        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
        table = spss.BasePivotTable("Warnings ", "Warnings")
        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel,)] = CellText.String(
            _("""An invalid variable has been specified. This command is not executed.""")
        )
        spss.EndProcedure()
    elif len(varnames) != 2:
        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
        table = spss.BasePivotTable("Warnings ", "Warnings")
        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel,)] = CellText.String(
            _("""Exactly two variables must be specified. This command is not executed.""")
        )
        spss.EndProcedure()
    else:
        try:
            warntext = []
            if cilevel < 50:
                warntext.append(_("CILEVEL cannot be less than 50%. It has been set to 50%."))
                cilevel = 50
            if cilevel > 99.999:
                warntext.append(_("CILEVEL cannot be greater than 99.999%. It has been set to 99.999%."))
                cilevel = 99.999
            if cilevel == int(cilevel):
                cilevel = int(cilevel)
            if wttype != 1:
                if wttype != 2:
                    warntext.append(_("WTTYPE must be 1 or 2. It has been set to 1."))
                    wttype = 1
            varlist = varnames[0] + " " + varnames[1]
            spss.Submit("PRESERVE.")
            tempdir = tempfile.gettempdir()
            spss.Submit("""CD "%s".""" % tempdir)
            wtvar = spss.GetWeightVar()
            if wtvar != None:
                spss.Submit(
                    r"""
COMPUTE %s=RND(%s)."""
                    % (wtvar, wtvar)
                )
                spss.Submit(
                    r"""
EXECUTE."""
                )
            maxloops = 2 * spss.GetCaseCount()
            spss.Submit("""SET PRINTBACK=OFF MPRINT=OFF MXLOOPS=%s.""" % maxloops)
            activeds = spss.ActiveDataset()
            if activeds == "*":
                activeds = "D" + str(random.uniform(0.1, 1))
                spss.Submit("DATASET NAME %s" % activeds)
            tmpvar1 = "V" + str(random.uniform(0.1, 1))
            tmpvar2 = "V" + str(random.uniform(0.1, 1))
            tmpvar3 = "V" + str(random.uniform(0.1, 1))
            tmpvar4 = "V" + str(random.uniform(0.1, 1))
            tmpvar5 = "V" + str(random.uniform(0.1, 1))
            tmpvar6 = "V" + str(random.uniform(0.1, 1))
            tmpdata1 = "D" + str(random.uniform(0.1, 1))
            tmpdata2 = "D" + str(random.uniform(0.1, 1))
            omstag1 = "T" + str(random.uniform(0.1, 1))
            omstag2 = "T" + str(random.uniform(0.1, 1))
            omstag3 = "T" + str(random.uniform(0.1, 1))
            omstag4 = "T" + str(random.uniform(0.1, 1))
            omstag5 = "T" + str(random.uniform(0.1, 1))
            omstag6 = "T" + str(random.uniform(0.1, 1))
            tmpfile1 = "F" + str(random.uniform(0.1, 1))
            tmpfile2 = "F" + str(random.uniform(0.1, 1))
            lowlabel = _("""Lower %s%% Asymptotic CI Bound""") % cilevel
            upplabel = _("""Upper %s%% Asymptotic CI Bound""") % cilevel
            spss.Submit(
                r"""
DATASET COPY %s WINDOW=HIDDEN."""
                % tmpdata1
            )
            spss.Submit(
                r"""
DATASET ACTIVATE %s WINDOW=ASIS."""
                % tmpdata1
            )
            filt = spssaux.GetSHOW("FILTER", olang="english")
            if filt != "No case filter is in effect":
                filtcond = filt.strip("(FILTER)")
                select = "SELECT IF " + str(filtcond) + "."
                spss.Submit("""%s""" % select)
                spss.Submit("""EXECUTE.""")
                spss.Submit("""USE ALL.""")
            banana = spssaux.getDatasetInfo(Info="SplitFile")
            if banana != "":
                warntext.append(_("This procedure ignores split file status."))
                spss.Submit(r"""SPLIT FILE OFF.""")
            spss.Submit(
                r"""
COUNT %s=%s (MISSING)."""
                % (tmpvar1, varlist)
            )
            spss.Submit(
                r"""
SELECT IF %s=0."""
                % tmpvar1
            )
            spss.Submit(
                r"""
EXECUTE."""
            )
            validn = spss.GetCaseCount()
            if validn < 2:
                spss.Submit(
                    r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                    % omstag1
                )
                spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                table = spss.BasePivotTable("Warnings ", "Warnings")
                table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel,)] = CellText.String(
                    _("""There are too few complete cases. This command is not executed.""")
                )
                spss.EndProcedure()
                spss.Submit(
                    r"""
OMSEND TAG = ['"%s"']."""
                    % omstag1
                )
            else:
                spss.Submit(
                    r"""
AGGREGATE
   /OUTFILE=* MODE=ADDVARIABLES
   /%s=SD(%s)
   /%s=SD(%s)."""
                    % (tmpvar2, varnames[0], tmpvar3, varnames[1])
                )
                try:
                    cur = spss.Cursor(isBinary=False)
                except:
                    cur = spss.Cursor()
                datarow = cur.fetchone()
                cur.close()
                sd1 = datarow[-2]
                sd2 = datarow[-1]
                if min(sd1, sd2) == 0:
                    spss.Submit(
                        r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                        % omstag1
                    )
                    spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                    table = spss.BasePivotTable("Warnings ", "Warnings")
                    table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                    rowLabel = CellText.String("1")
                    table[(rowLabel,)] = CellText.String(
                        _("""All ratings are the same for at least one rater. This command is not executed.""")
                    )
                    spss.EndProcedure()
                    spss.Submit(
                        r"""
OMSEND TAG = ['"%s"']."""
                        % omstag1
                    )
                else:
                    if len(warntext) > 0:
                        spss.Submit(
                            r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                            % omstag1
                        )
                        if len(warntext) == 1:
                            spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                            table = spss.BasePivotTable("Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel,)] = CellText.String("%s" % warntext[0])
                            spss.EndProcedure()
                        if len(warntext) == 2:
                            spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                            table = spss.BasePivotTable("Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel,)] = CellText.String("%s \n" "%s" % (warntext[0], warntext[1]))
                            spss.EndProcedure()
                        if len(warntext) == 3:
                            spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                            table = spss.BasePivotTable("Warnings ", "Warnings")
                            table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                            rowLabel = CellText.String("1")
                            table[(rowLabel,)] = CellText.String(
                                "%s \n" "%s \n" "%s" % (warntext[0], warntext[1], warntext[2])
                            )
                            spss.EndProcedure()
                        spss.Submit(
                            r"""
OMSEND TAG = ['"%s"']."""
                            % omstag1
                        )
                    spss.Submit(
                        r"""
DELETE VARIABLES %s %s."""
                        % (tmpvar2, tmpvar3)
                    )
                    spss.Submit(
                        r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N."""
                        % (tmpfile1, varlist, tmpvar4)
                    )
                    spss.Submit(
                        r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Variables to Cases'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                        % omstag2
                    )
                    spss.Submit(
                        r"""
VARSTOCASES
  /MAKE %s FROM %s."""
                        % (tmpvar5, varlist)
                    )
                    spss.Submit(
                        r"""
OMSEND TAG = ['"%s"']."""
                        % omstag2
                    )
                    catdata = []
                    try:
                        cur = spss.Cursor(isBinary=False)
                    except:
                        cur = spss.Cursor()
                    while True:
                        datarow = cur.fetchone()
                        if datarow is None:
                            break
                        catdata.append(datarow[-1])
                    cur.close()
                    cats = list(set(catdata))
                    cattest = 0
                    if any(item != round(item) for item in cats):
                        cattest = 1
                        spss.Submit(
                            r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                            % omstag1
                        )
                        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                        table = spss.BasePivotTable("Warnings ", "Warnings")
                        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel,)] = CellText.String(
                            _("""Some ratings are not integers. This command is not executed.""")
                        )
                        spss.EndProcedure()
                        spss.Submit(
                            r"""
OMSEND TAG = ['"%s"']."""
                            % omstag1
                        )
                    elif min(cats) < 1.0:
                        spss.Submit(
                            r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                            % omstag1
                        )
                        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                        table = spss.BasePivotTable("Warnings ", "Warnings")
                        table.Append(spss.Dimension.Place.row, "rowdim", hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel,)] = CellText.String(
                            _("""Some ratings are less than 1. This command is not executed.""")
                        )
                        spss.EndProcedure()
                        spss.Submit(
                            r"""
OMSEND TAG = ['"%s"']."""
                            % omstag1
                        )
                    else:
                        spss.Submit(
                            r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N."""
                            % (tmpfile2, tmpvar5, tmpvar6)
                        )
                        spss.Submit(
                            r"""
DATASET DECLARE %s WINDOW=HIDDEN"""
                            % tmpdata2
                        )
                        spss.Submit(
                            r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Matrix'] 
 /DESTINATION VIEWER=NO
 /TAG='"%s"'."""
                            % omstag3
                        )
                        spss.Submit(
                            r"""
MATRIX.
GET x 
  /FILE=%s
  /VARIABLES=%s %s.
GET ratecats
  /FILE=%s
  /VARIABLES=%s.
COMPUTE size=MMAX(ratecats).
COMPUTE y=MAKE(size,size,0).
LOOP i=1 to NROW(y).
+ LOOP j=1 to NCOL(y).
+   LOOP k=1 to NROW(x).
+     DO IF (x(k,1)=i and x(k,2)=j).
+       COMPUTE y(i,j)=x(k,3).
+     END IF.
+   END LOOP.
+ END LOOP.
END LOOP.
COMPUTE wttype=%s.
COMPUTE wt=MAKE(NROW(y),NCOL(y),0).
LOOP i=1 to NROW(y).
+ LOOP j=1 to NCOL(y).
+   DO IF wttype=1.
+     COMPUTE wt(i,j)=1-(ABS(i-j)/(size-1)).
+   ELSE IF wttype=2.
+     COMPUTE wt(i,j)=1-((i-j)/(NROW(y)-1))**2.
+   END IF.
+ END LOOP.
END LOOP.
COMPUTE n=MSUM(y).
COMPUTE prop=y/n.
COMPUTE p_i=RSUM(prop).
COMPUTE p_j=CSUM(prop).
COMPUTE w_i=(wt*T(p_j))*MAKE(1,size,1).
COMPUTE w_j=MAKE(size,1,1)*(T(p_i)*wt).
COMPUTE po=MSUM(wt&*prop).
COMPUTE pe=MSUM(MDIAG(p_i)*wt*MDIAG(p_j)).
COMPUTE kstat=(po-pe)/(1-pe).
COMPUTE var0=(T(p_i)*((wt-(w_i+w_j))&**2)*T(p_j)-pe**2)/(n*(1-pe)**2).
DO IF var0>=0.
+ COMPUTE ase0=SQRT(var0).
ELSE.
+ COMPUTE ase0=-1.
END IF.
DO IF ase0>0.
+ COMPUTE z=kstat/ase0.
+ COMPUTE sig=1-CHICDF(z**2,1).
ELSE.
+ COMPUTE z=-1.
+ COMPUTE sig=-1.
END IF.
COMPUTE var1=(MSUM((prop&*((wt-(w_i+w_j)&*(1-kstat))&**2)))-(kstat-pe*(1-kstat))**2)/(n*(1-pe)**2).
DO IF var1>=0.
+ COMPUTE ase1=SQRT(var1).
ELSE.
+ COMPUTE ase1=-1.
END IF.
SAVE {wttype,kstat,ase1,z,sig,ase0}
   /OUTFILE=%s
   /VARIABLES=wttype,kstat,ase1,z,sig,ase0.
END MATRIX."""
                            % (tmpfile1, varlist, tmpvar4, tmpfile2, tmpvar5, wttype, tmpdata2)
                        )
                        spss.Submit(
                            r"""
OMSEND TAG=['"%s"']."""
                            % omstag3
                        )
                        spss.Submit(
                            r"""
DATASET ACTIVATE %s WINDOW=ASIS."""
                            % tmpdata2
                        )
                        spss.Submit(
                            r"""
DO IF ase0=-1.
+ RECODE z sig (-1=SYSMIS).
END IF.
EXECUTE.
DELETE VARIABLES ase0.
RECODE ase1 (-1=SYSMIS).
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase1."""
                            % cilevel
                        )
                        spss.Submit(
                            r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase1."""
                            % cilevel
                        )
                        spss.Submit(
                            r"""
FORMATS kstat ase1 z sig lower upper (F11.3).
VARIABLE LABELS kstat %s."""
                            % _smartquote(_("""Kappa"""))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS ase1 %s."""
                            % _smartquote(_("""Asymptotic Standard Error"""))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS z %s."""
                            % _smartquote(_("""Z"""))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS sig %s. """
                            % _smartquote(_("""P Value"""))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS lower %s. """
                            % _smartquote(_(lowlabel))
                        )
                        spss.Submit(
                            r"""
VARIABLE LABELS upper %s. """
                            % _smartquote(_(upplabel))
                        )
                        if wttype == 1:
                            spss.Submit(
                                r"""
VARIABLE LABELS wttype %s."""
                                % _smartquote(_("""Linear"""))
                            )
                        if wttype == 2:
                            spss.Submit(
                                r"""
VARIABLE LABELS wttype %s."""
                                % _smartquote(_("""Quadratic"""))
                            )
                        spss.Submit(
                            r"""
EXECUTE.
"""
                        )
                        spss.Submit(
                            r"""
OMS
  /SELECT TABLES 
  /IF COMMANDS=['Weighted Kappa'] SUBTYPES=['Notes']
  /DESTINATION VIEWER=NO
  /TAG = '"%s"'."""
                            % omstag4
                        )
                        spss.Submit(
                            r"""
OMS
  /SELECT TEXTS
  /IF COMMANDS=['Weighted Kappa'] LABELS=['Active Dataset']
  /DESTINATION VIEWER=NO
  /TAG = '"%s"'."""
                            % omstag5
                        )
                        if len(warntext) > 0:
                            spss.Submit(
                                r"""
OMS
 /SELECT HEADINGS
 /IF COMMANDS=['Weighted Kappa']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'."""
                                % omstag6
                            )
                        try:
                            cur = spss.Cursor(isBinary=False)
                        except:
                            cur = spss.Cursor()
                        data = cur.fetchone()
                        cur.close()
                        spss.StartProcedure(_("Weighted Kappa"), "Weighted Kappa")
                        table = spss.BasePivotTable(_("Weighted Kappa"), "Kappa", caption=caption)
                        table.SimplePivotTable(
                            rowdim=_("Weighting"),
                            rowlabels=[CellText.String(spss.GetVariableLabel(0))],
                            coldim="",
                            collabels=[
                                spss.GetVariableLabel(1),
                                spss.GetVariableLabel(2),
                                spss.GetVariableLabel(3),
                                spss.GetVariableLabel(4),
                                spss.GetVariableLabel(5),
                                spss.GetVariableLabel(6),
                            ],
                            cells=[data[1], data[2], data[3], data[4], data[5], data[6]],
                        )
                        spss.EndProcedure()
                        if len(warntext) > 0:
                            spss.Submit(
                                r"""
OMSEND TAG = ['"%s"']."""
                                % omstag6
                            )
        finally:
            try:
                spss.Submit(
                    r"""
DATASET CLOSE %s."""
                    % tmpdata1
                )
                spss.Submit(
                    r"""
DATASET ACTIVATE %s WINDOW=ASIS."""
                    % activeds
                )
                if validn >= 2:
                    if min(sd1, sd2) > 0:
                        if cattest == 0:
                            if min(cats) >= 1:
                                spss.Submit(
                                    r"""
OMSEND TAG=['"%s"' '"%s"']."""
                                    % (omstag4, omstag5)
                                )
                                spss.Submit(
                                    r"""
DATASET CLOSE %s."""
                                    % tmpdata2
                                )
                                spss.Submit(
                                    r"""
ERASE FILE=%s."""
                                    % tmpfile2
                                )
                        spss.Submit(
                            r"""
ERASE FILE=%s."""
                            % tmpfile1
                        )
            except:
                pass
            spss.Submit(
                r"""
RESTORE.
"""
            )
def fleisskappaextension(variables, cilevel=95):
    
    varnames = expandvarnames(variables)
    vardict = spssaux.VariableDict(varnames)
    if len(vardict) != len(varnames):
        spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
        table = spss.BasePivotTable("Warnings ","Warnings")
        table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel,)] = CellText.String(_("""An invalid variable has been specified. This command is not executed."""))
        spss.EndProcedure()
    elif len(varnames) < 2:
        spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
        table = spss.BasePivotTable("Warnings ","Warnings")
        table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
        rowLabel = CellText.String("1")
        table[(rowLabel,)] = CellText.String(_("""At least two variables must be specified. This command is not executed."""))
        spss.EndProcedure()
        
    else:
        try:
            warntext = []
            if cilevel < 50:
                warntext.append(_("CILEVEL cannot be less than 50%. It has been reset to 50%."))
                cilevel = 50
            if cilevel > 99.999:
                warntext.append(_("CILEVEL cannot be greater than 99.999%. It has been reset to 99.999%."))
                cilevel = 99.999
            if cilevel == int(cilevel):
                cilevel = int(cilevel)
            varlist = varnames[0]
            for i in range(1,len(varnames)):
                varlist = varlist + ' ' + varnames[i]
            spss.Submit("PRESERVE.")
            tempdir = tempfile.gettempdir()
            spss.Submit("""CD "%s".""" % tempdir)
            wtvar = spss.GetWeightVar()
            if wtvar != None:
                spss.Submit(r"""
COMPUTE %s=RND(%s).""" % (wtvar, wtvar))
                spss.Submit(r"""
EXECUTE.""")
                wtdn = GetWeightSum(varnames)
            else:
                wtdn = spss.GetCaseCount()
            maxloops = wtdn + 1
            spss.Submit("""SET PRINTBACK=OFF MPRINT=OFF OATTRS=ENG MXLOOPS=%s.""" % maxloops)
            activeds = spss.ActiveDataset()
            if activeds == "*":
                activeds = "D" + str(random.uniform(.1,1))
                spss.Submit("DATASET NAME %s" % activeds)
            tmpvar1 = "V" + str(random.uniform(.1,1))
            tmpvar2 = "V" + str(random.uniform(.1,1))
            tmpvar3 = "V" + str(random.uniform(.1,1))
            tmpfile1 = "F" + str(random.uniform(.1,1))
            tmpfile2 = "F" + str(random.uniform(.1,1))
            tmpdata1 = "D" + str(random.uniform(.1,1))
            tmpdata2 = "D" + str(random.uniform(.1,1))
            tmpdata3 = "D" + str(random.uniform(.1,1))
            omstag1 = "T" + str(random.uniform(.1,1))
            omstag2 = "T" + str(random.uniform(.1,1))
            omstag3 = "T" + str(random.uniform(.1,1))
            omstag4 = "T" + str(random.uniform(.1,1))
            omstag5 = "T" + str(random.uniform(.1,1))
            omstag6 = "T" + str(random.uniform(.1,1))
            lowlabel = _("""Lower %s%% Asymptotic CI Bound""") % cilevel
            upplabel = _("""Upper %s%% Asymptotic CI Bound""") % cilevel
            spss.Submit(r"""
DATASET COPY %s WINDOW=HIDDEN.""" % tmpdata1)
            spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata1)
            filt = spssaux.GetSHOW("FILTER", olang="english")
            if filt != "No case filter is in effect":
                filtcond = filt.strip("(FILTER)")
                select = "SELECT IF " + str(filtcond) + "."
                spss.Submit("""%s""" % select)
                spss.Submit("""EXECUTE.""")
                spss.Submit("""USE ALL.""")
            banana = spssaux.getDatasetInfo(Info="SplitFile")
            if banana != "":
                warntext.append(_("This command ignores split file status."))
                spss.Submit(r"""SPLIT FILE OFF.""")   
            spss.Submit(r"""
COUNT %s=%s (MISSING).""" % (tmpvar1, varlist))
            spss.Submit(r"""
SELECT IF %s=0.""" % tmpvar1)
            spss.Submit(r"""
EXECUTE.""")
            validn=spss.GetCaseCount()
            if wtvar == None:
                spss.Submit(r"""
SAVE OUTFILE=%s.""" % tmpfile1)
            else:
                spss.Submit(r"""
DO IF %s >= 1.""" % wtvar)
                spss.Submit(r"""
+ LOOP #i=1 TO %s.""" % wtvar)
                spss.Submit(r"""
XSAVE OUTFILE=%s
  /KEEP=%s
  /DROP=%s.""" % (tmpfile1, varlist, wtvar))
                spss.Submit(r"""
+ END LOOP.
END IF.
EXECUTE.
""")   
            spss.Submit(r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Variables to Cases'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag1)
            spss.Submit(r"""
VARSTOCASES
  /MAKE %s FROM %s.""" % (tmpvar2, varlist))
            spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag1)
            catdata = []
            try:
	        cur = spss.Cursor(isBinary=False)
            except:
	        cur = spss.Cursor()
            while True:
                datarow = cur.fetchone()
                if datarow is None:
                    break
                catdata.append(datarow[-1])
            cur.close()
            cats = list(set(catdata))
            ncats = len(cats)
            nraters = len(varnames)
            neededn = max(ncats, nraters)
            if validn < neededn:
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                table = spss.BasePivotTable("Warnings ","Warnings")
                table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel,)] = CellText.String(_("""There are too few complete cases. This command is not executed."""))
                spss.EndProcedure()
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
            elif ncats < 2:
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                table = spss.BasePivotTable("Warnings ","Warnings")
                table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
                rowLabel = CellText.String("1")
                table[(rowLabel,)] = CellText.String(_("""All ratings are the same. This command is not executed."""))
                spss.EndProcedure()
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
            else:
                if len(warntext) > 0:
                    spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag2)
                    if len(warntext) == 1:
                        spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                        table = spss.BasePivotTable("Warnings ","Warnings")
                        table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel,)] = CellText.String("%s" % warntext[0])
                        spss.EndProcedure()
                    if len(warntext) == 2:
                        spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                        table = spss.BasePivotTable("Warnings ","Warnings")
                        table.Append(spss.Dimension.Place.row,"rowdim",hideLabels=True)
                        rowLabel = CellText.String("1")
                        table[(rowLabel,)] = CellText.String("%s \n" "%s" % (warntext[0], warntext[1]))
                        spss.EndProcedure()
                    spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag2)
                spss.Submit(r"""
AGGREGATE
  /OUTFILE=%s
  /BREAK=%s
  /%s=N.""" % (tmpfile2, tmpvar2, tmpvar3))
                spss.Submit(r"""
DATASET DECLARE %s WINDOW=HIDDEN.""" % tmpdata2)
                spss.Submit(r"""
DATASET DECLARE %s WINDOW=HIDDEN.""" % tmpdata3)
                spss.Submit(r"""
OMS /SELECT ALL EXCEPT=WARNINGS 
 /IF COMMANDS=['Matrix'] 
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag3)
                spss.Submit(r"""
MATRIX.
GET x 
  /FILE=%s
  /VARIABLES=%s.
GET ratecats
  /FILE=%s
  /VARIABLES=%s.
COMPUTE n=NROW(x).
COMPUTE c=NROW(ratecats).
COMPUTE y=MAKE(n,c,0).
LOOP i=1 to n.
+ LOOP j=1 to NCOL(x).
+   LOOP k=1 to c.
+     DO IF x(i,j)=ratecats(k).
+       COMPUTE y(i,k)=y(i,k)+1.
+     END IF.
+   END LOOP.
+ END LOOP.
END LOOP.
COMPUTE k=NCOL(x).
COMPUTE pe=MSUM((CSUM(y)/MSUM(y))&**2).
COMPUTE pa=MSSQ(y)/(NROW(y)*k*(k-1))-(1/(k-1)).
COMPUTE kstat=(pa-pe)/(1-pe).
COMPUTE cp=(CSSQ(y)-CSUM(y))&/((k-1)&*CSUM(y)).
COMPUTE pj=CSUM(y)/MSUM(y).
COMPUTE one=MAKE(1,NCOL(pj),1).
COMPUTE qj=one-pj.
COMPUTE kj=(cp-pj)&/qj.
COMPUTE num=2*((pj*t(qj))**2-MSUM(pj&*qj&*(qj-pj))).
COMPUTE den=n*k*(k-1)*((pj*t(qj))**2).
COMPUTE ase=SQRT(num/den).
COMPUTE z=kstat/ase.
COMPUTE sig=1-CHICDF(z**2,1).
SAVE {kstat,ase,z,sig}
   /OUTFILE=%s
   /VARIABLES=kstat,ase,z,sig.
COMPUTE asej=MAKE(1,c,SQRT(2/(n*k*(k-1)))).
COMPUTE zj=kj&/asej.
COMPUTE sigj=one-CHICDF(zj&**2,1).
SAVE {ratecats,t(cp),t(kj),t(asej),t(zj),t(sigj)}
  /OUTFILE=%s
  /VARIABLES=category,cp,kstat,ase,z,sig.
END MATRIX.""" % (tmpfile1, varlist, tmpfile2, tmpvar2, tmpdata2, tmpdata3))
                spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag3)
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata2)
                spss.Submit(r"""
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
FORMATS kstat ase z sig lower upper (F11.3).
VARIABLE LABELS kstat %s. """ % _smartquote(_("""Kappa""")))
                spss.Submit(r"""
VARIABLE LABELS ase %s. """ % _smartquote(_("""Asymptotic Standard Error""")))
                spss.Submit(r"""
VARIABLE LABELS z %s. """ % _smartquote(_("""Z""")))
                spss.Submit(r"""
VARIABLE LABELS sig %s. """ % _smartquote(_("""P Value""")))
                spss.Submit(r"""
VARIABLE LABELS lower %s. """ % _smartquote(_(lowlabel)))
                spss.Submit(r"""
VARIABLE LABELS upper %s. """ % _smartquote(_(upplabel)))
                spss.Submit(r"""
EXECUTE.
""")
                try:
	            cur = spss.Cursor(isBinary=False)
                except:
	            cur = spss.Cursor()
                data1=cur.fetchone()
                cur.close()
                collabels1=[spss.GetVariableLabel(0),spss.GetVariableLabel(1),spss.GetVariableLabel(2),spss.GetVariableLabel(3), \
                                         spss.GetVariableLabel(4),spss.GetVariableLabel(5)]
                celldata1=[data1[0],data1[1],data1[2],data1[3],data1[4],data1[5]]
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % tmpdata3)
                spss.Submit(r"""
COMPUTE lower=kstat-SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
COMPUTE upper=kstat+SQRT(IDF.CHISQUARE(%s/100,1))*ase.""" % cilevel)
                spss.Submit(r"""
FORMATS category (F10.0) cp kstat ase z sig lower upper (F11.3).
VARIABLE LABELS category %s. """ % _smartquote(_("""Rating Category""")))
                spss.Submit(r"""
VARIABLE LABELS cp %s. """ % _smartquote(_("""Conditional Probability""")))
                spss.Submit(r"""
VARIABLE LABELS kstat %s. """ % _smartquote(_("""Kappa""")))
                spss.Submit(r"""
VARIABLE LABELS ase %s. """ % _smartquote(_("""Asymptotic Standard Error""")))
                spss.Submit(r"""                
VARIABLE LABELS z %s. """ % _smartquote(_("""Z""")))
                spss.Submit(r""" 
VARIABLE LABELS sig %s. """ % _smartquote(_("""P Value""")))
                spss.Submit(r"""
VARIABLE LABELS lower %s. """ % _smartquote(_(lowlabel)))
                spss.Submit(r"""
VARIABLE LABELS upper %s. """ % _smartquote(_(upplabel)))
                spss.Submit(r""" 
EXECUTE.""")
                spss.Submit(r"""
OMS
 /SELECT TABLES
 /IF COMMANDS=['Fleiss Kappa'] SUBTYPES=['Notes']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag4)
                spss.Submit(r"""
OMS
 /SELECT TEXTS
 /IF COMMANDS=['Fleiss Kappa'] LABELS=['Active Dataset']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag5)
                if len(warntext) > 0:
                    spss.Submit(r"""
OMS
 /SELECT HEADINGS
 /IF COMMANDS=['Fleiss Kappa']
 /DESTINATION VIEWER=NO
 /TAG = '"%s"'.""" % omstag6)
                n=spss.GetCaseCount
                rlabels=[]
                data2=[]
                try:
	            cur = spss.Cursor(isBinary=False)
                except:
	            cur = spss.Cursor()
                for i in range(0,spss.GetCaseCount()):
                    datarow=cur.fetchone()
                    data2.append(datarow[1:])
                    rlabels.append(datarow[0])
                cur.close()
                def _flatten(seq):
                    for item in seq:
                        if spssaux._isseq(item):
                            for subitem in _flatten(item):
                                yield subitem
                        else:
                            yield item
                data2=[item for item in _flatten(data2)]
                spss.StartProcedure(_("Fleiss Kappa"),"Fleiss Kappa")
                table1 = spss.BasePivotTable(_("Overall Kappa"),"Overall Kappa")
                table1.SimplePivotTable(rowdim = _(""),
                       rowlabels=[CellText.String("Overall")],
                       coldim="",
                       collabels=collabels1,
                       cells=celldata1)
                if any(item != round(item) for item in rlabels):
                    caption=(_("Non-integer rating category values are truncated for presentation."))
                else:
                    caption=("")
                table2=spss.BasePivotTable(_("Kappas for Individual Categories"),
                                _("Individual Category Kappa Statistics"),caption=caption)
                rowlabels=[(CellText.String("{:>9.0f}".format(rlabels[i]))) for i in range(len(rlabels))]
                collabels=[spss.GetVariableLabel(1),spss.GetVariableLabel(2),spss.GetVariableLabel(3), \
                      spss.GetVariableLabel(4),spss.GetVariableLabel(5),spss.GetVariableLabel(6), \
                      spss.GetVariableLabel(7)]
                table2.SimplePivotTable(rowdim=_("  Rating Category"),
                      rowlabels=rowlabels,
                      coldim="",
                      collabels=collabels,
                      cells=data2)
                spss.EndProcedure()
                if len(warntext) > 0:
                    spss.Submit(r"""
OMSEND TAG = ['"%s"'].""" % omstag6)
        finally:
            try:
                spss.Submit("""
DATASET CLOSE %s.""" % tmpdata1)
                spss.Submit(r"""
DATASET ACTIVATE %s WINDOW=ASIS.""" % activeds)
                if validn >= neededn:
                    if ncats >= 2:
                        spss.Submit("""
OMSEND TAG=['"%s"' '"%s"'].""" % (omstag4,omstag5))
                        spss.Submit("""
DATASET CLOSE %s.""" % tmpdata2)
                        spss.Submit("""
DATASET CLOSE %s.""" % tmpdata3)
                        spss.Submit("""
ERASE FILE=%s.""" % tmpfile1)
                        spss.Submit(r"""
ERASE FILE=%s.""" % tmpfile2)
            except:
                pass
            spss.Submit("""
RESTORE.
""")