def washobj():
    dat = htsip.xmlsessread()
    confdat = htsip.confread(dat["file"])
    modify = request.form["modify"]
    name = request.form["trgpname"]
    if request.form["subval"] == "delete":
        confdat = htsip.confdatadelete(dat["file"], int(request.form["objnumber"]))
    else:
        ld = "y"
        for cn in confdat["objlist"]:
            if cn["name"] == name:
                if modify == "no":
                    ld = "n"
        if ld == "n":
            return render_template(
                "gui.img.edit.html", dat=dat, confdat=confdat, act="newobject", error="y", objname=name
            )
        else:
            trgpx = request.form["trgpx"]
            trgpy = request.form["trgpy"]
            trgpz = request.form["trgpz"]
            dimx = request.form["dimx"]
            dimy = request.form["dimy"]
            flrt = request.form["flrt"]
            wshtm = request.form["wshtm"]
            if modify == "no":
                confdat["objlist"].append(
                    {
                        "name": name,
                        "x": float(trgpx),
                        "y": float(trgpy),
                        "dimx": float(dimx),
                        "dimy": float(dimy),
                        "z": float(trgpz),
                        "flrt": float(flrt),
                        "wshtm": float(wshtm),
                        "typ": "wash",
                    }
                )
            if modify == "yes":
                objnumber = int(request.form["objnumber"])
                confdat["objlist"][objnumber]["x"] = float(trgpx)
                confdat["objlist"][objnumber]["y"] = float(trgpy)
                confdat["objlist"][objnumber]["z"] = float(trgpz)
                confdat["objlist"][objnumber]["dimx"] = float(dimx)
                confdat["objlist"][objnumber]["dimy"] = float(dimy)
                confdat["objlist"][objnumber]["flrt"] = float(flrt)
                confdat["objlist"][objnumber]["wshtm"] = float(wshtm)
    htsip.confwrite(dat["file"], confdat)
    ti = str(time.time())
    mod = "n"
    if len(confdat["objlist"]) > 0:
        mod = "y"
    return render_template("gui.img.edit.html", dat=dat, confdat=confdat, act="plotarea", error="n", mod=mod)
def pointobj():
    if request.method == "POST":
        dat = htsip.xmlsessread()
        confdat = htsip.confread(dat["file"])
        objname = request.form["name"]
        confdat = newobjlist(confdat)
        if objname in confdat:
            return render_template("gui.img.edit.html", dat=dat, confdat=confdat, act="newobject", error="y")
        else:
            x = request.form["x"]
            y = request.form["y"]
            z = request.form["z"]
            confdat["objlist"].append({"name": objname, "x": float(x), "y": float(y), "z": float(z)})
        htsip.confwrite(dat["file"], confdat)
        ti = str(time.time())
        htsi.drawpoint(dat, objname)
        mod = "n"
        if len(confdat["objlist"]) > 0:
            mod = "y"
        return render_template("gui.img.edit.html", dat=dat, confdat=confdat, act="plotarea", ti=ti, mod=mod)
def plotarea():
    if request.method == "POST":
        dat = htsip.xmlsessread()
        confdat = htsip.confread(dat["file"])
        # Plot area dimensions
        if len(request.form["len"]) > 0:
            confdat["deck"]["plx"] = float(request.form["len"])
        if len(request.form["wid"]) > 0:
            confdat["deck"]["ply"] = float(request.form["wid"])
        dat["displothead"] = int(request.form["displothead"])
        dat["disobjs"] = int(request.form["disobjs"])
        # Display plothead position
        if len(request.form["pxpos"]) > 0:
            dat["pxpos"] = request.form["pxpos"]
        if len(request.form["pypos"]) > 0:
            dat["pypos"] = request.form["pypos"]
        htsip.sessconfwrite(dat)
        htsip.confwrite(dat["file"], confdat)
        ti = str(time.time())
        mod = "n"
        if len(confdat["objlist"]) > 0:
            mod = "y"
        return render_template("gui.img.edit.html", dat=dat, act="plotarea", confdat=confdat, ti=ti, mod=mod)
def plothead():
    if request.method == "POST":
        dat = htsip.xmlsessread()
        confdat = htsip.confread(dat["file"])
        if len(request.form["len"]) > 0:
            confdat["plothead"]["x"] = request.form["len"]
        if len(request.form["wid"]) > 0:
            confdat["plothead"]["y"] = request.form["wid"]
        if len(request.form["posx"]) > 0:
            confdat["plothead"]["posx"] = request.form["posx"]
        if len(request.form["posy"]) > 0:
            confdat["plothead"]["posy"] = request.form["posy"]
        if len(request.form["xsp"]) > 0:
            confdat["plothead"]["xsp"] = request.form["xsp"]
        if len(request.form["ysp"]) > 0:
            confdat["plothead"]["ysp"] = request.form["ysp"]
        if len(request.form["tipx"]) > 0:
            confdat["plothead"]["tipx"] = float(request.form["tipx"])
        if len(request.form["tipy"]) > 0:
            confdat["plothead"]["tipy"] = float(request.form["tipy"])
        if len(request.form["reftip"]) > 0:
            confdat["plothead"]["reftip"] = int(request.form["reftip"])
        if len(request.form["camx"]) > 0:
            confdat["plothead"]["camx"] = request.form["camx"]
        if len(request.form["camy"]) > 0:
            confdat["plothead"]["camy"] = request.form["camy"]
        if len(request.form["camdiam"]) > 0:
            confdat["plothead"]["camdiam"] = request.form["camdiam"]
        dat["img"] = request.form["img"]
        htsip.sessconfwrite(dat)
        htsip.confwrite(dat["file"], confdat)
        ti = str(time.time())
        mod = "n"
        if len(confdat["objlist"]) > 0:
            mod = "y"
        return render_template("gui.img.edit.html", dat=dat, act="plothead", confdat=confdat, ti=ti, mod=mod)
def microwellobj():
    dat = htsip.xmlsessread()
    modify = request.form["modify"]
    confdat = htsip.confread(dat["file"])
    name = request.form["trgpname"]
    if request.form["subval"] == "delete":
        confdat = htsip.confdatadelete(dat["file"], int(request.form["objnumber"]))
    else:
        ld = "y"
        for cn in confdat["objlist"]:
            if cn["name"] == name:
                if modify == "no":
                    ld = "n"
        if ld == "n":
            return render_template(
                "gui.img.edit.html", dat=dat, confdat=confdat, act="newobject", error="y", objname=name
            )
        else:
            orient = request.form["orient"]
            trgpx = request.form["trgpx"]
            trgpy = request.form["trgpy"]
            trgpz = request.form["trgpz"]
            dimx = request.form["dimx"]
            dimy = request.form["dimy"]
            wllrow = request.form["wllrow"]
            wllrowsp = request.form["wllrowsp"]
            wllcol = request.form["wllcol"]
            wllcolsp = request.form["wllcolsp"]
            if modify == "no":
                confdat["objlist"].append(
                    {
                        "name": name,
                        "x": float(trgpx),
                        "y": float(trgpy),
                        "dimx": float(dimx),
                        "dimy": float(dimy),
                        "z": float(trgpz),
                        "wllrow": int(wllrow),
                        "wllrowsp": float(wllrowsp),
                        "wllcol": int(wllcol),
                        "wllcolsp": float(wllcolsp),
                        "typ": "microwell",
                        "orient": orient,
                    }
                )
            if modify == "yes":
                objnumber = int(request.form["objnumber"])
                confdat["objlist"][objnumber]["orient"] = orient
                confdat["objlist"][objnumber]["name"] = name
                confdat["objlist"][objnumber]["x"] = float(trgpx)
                confdat["objlist"][objnumber]["y"] = float(trgpy)
                confdat["objlist"][objnumber]["z"] = float(trgpz)
                confdat["objlist"][objnumber]["dimx"] = float(dimx)
                confdat["objlist"][objnumber]["dimy"] = float(dimy)
                confdat["objlist"][objnumber]["wllrow"] = int(wllrow)
                confdat["objlist"][objnumber]["wllrowsp"] = float(wllrowsp)
                confdat["objlist"][objnumber]["wllcol"] = int(wllcol)
                confdat["objlist"][objnumber]["wllcolsp"] = float(wllcolsp)
            htsip.confwrite(dat["file"], confdat)
    ti = str(time.time())
    mod = "n"
    if len(confdat["objlist"]) > 0:
        mod = "y"
    return render_template("gui.img.edit.html", dat=dat, confdat=confdat, act="plotarea", error="n", mod=mod)