' '---------------------------- '--- Creation information --- '---------------------------- ' 'Name: calcep.ave 'Version: 1.1 'Date: 02/16/97 'Author: Ferdi Hellweger ' Center for Research in Water Resources ' The University of Texas at Austin ' ferdi@crwr.utexas.edu ' '--------------------------- '--- Purpose/Description --- '--------------------------- ' 'This program computes the bulk dispersion coefficient. ' '--------------------------------------------- '--- Check if units configuration happened --- '--------------------------------------------- ' if (not _configu) then configure = msgbox.yesno("Units are not configured. Configure it now?","BALANCE",true) if (configure) then av.run("balconu", nil) else exit end end ' '---------------- '--- Get view --- '---------------- ' theview = av.getactivedoc ' '------------------ '--- Get themes --- '------------------ ' theactivethemes = theview.getactivethemes if (theactivethemes.count = 0) then msgbox.error("No active themes found", "BALANCE") exit end if (theactivethemes.count = 1) then msgbox.error("Only one active theme found", "BALANCE") exit end if (theactivethemes.count > 2) then msgbox.error("Too many active themes found", "BALANCE") exit end if (theactivethemes.count = 2) then lfound = false pfound = false for each activetheme in theactivethemes theftab = activetheme.getftab theshapef = theftab.findfield("shape") theshape = theftab.returnvalue(theshapef,0) if (theshape.getclass.getclassname = "polyline") then theltheme = activetheme lfound = true end if (theshape.getclass.getclassname = "polygon") then theptheme = activetheme pfound = true end end end if (not lfound) then msgbox.error("One theme needs to be a line theme", "BALANCE") exit end if (not pfound) then msgbox.error("One theme needs to be a polygon theme", "BALANCE") exit end ' '------------------------------ '--- Get calculation option --- '------------------------------ ' choices = list.make choices = choices.add("Area Field, a") choices = choices.add("Depth Field, h and length") ' mychoice = msgbox.listasstring(choices, "Take area from", "BALANCE") if (mychoice = nil) then exit end ' '--------------------- '--- Set up themes --- '--------------------- ' 'line theme ' lftab = theltheme.getftab if (lftab = nil) then msgbox.error("Can't open line theme","BALANCE") exit end ' lshapef = lftab.findfield("shape") if (lshapef = nil) then msgbox.error("Can't find 'shape' field in line theme","BALANCE") exit end ' lpf = lftab.findfield("lpoly#") if (lpf = nil) then lpf = lftab.findfield("lpoly_") end if (lpf = nil) then msgbox.error("Can't find 'lpoly#' field in line theme","BALANCE") exit end ' rpf = lftab.findfield("rpoly#") if (rpf = nil) then rpf = lftab.findfield("rpoly_") end if (rpf = nil) then msgbox.error("Can't find 'rpoly#' field in line theme","BALANCE") exit end ' ef = lftab.findfield("e") if (ef = nil) then addfield = msgbox.yesno("Can't find 'e' field in line theme. Add it?","BALANCE", true) if (addfield) then lftab.seteditable(true) ef = field.make("e", #FIELD_DECIMAL, 16, 4) lftab.addfields({ef}) lftab.seteditable(false) else exit end end ' epf = lftab.findfield("ep") if (epf = nil) then addfield = msgbox.yesno("Can't find 'ep' field in line theme. Add it?","BALANCE", true) if (addfield) then lftab.seteditable(true) ef = field.make("ep", #FIELD_DECIMAL, 16, 4) lftab.addfields({epf}) lftab.seteditable(false) else exit end end ' if (mychoice = "Area Field, a") then af = lftab.findfield("a") if (af = nil) then addfield = msgbox.yesno("Can't find 'a' field in line theme. Add it?","BALANCE", true) if (addfield) then lftab.seteditable(true) epf = field.make("a", #FIELD_DECIMAL, 16, 4) lftab.addfields({af}) lftab.seteditable(false) else exit end end end if (mychoice = "Depth Field, h and length") then hf = lftab.findfield("h") if (hf = nil) then addfield = msgbox.yesno("Can't find 'h' field in line theme. Add it?","BALANCE", true) if (addfield) then lftab.seteditable(true) hf = field.make("h", #FIELD_DECIMAL, 16, 4) lftab.addfields({hf}) lftab.seteditable(false) else exit end end end ' 'polygon theme ' pftab = theptheme.getftab if (pftab = nil) then msgbox.error("Can't open polygon theme","BALANCE") exit end ' pshapef = pftab.findfield("shape") if (pshapef = nil) then msgbox.error("Can't find 'shape' field in polygon theme","BALANCE") exit end ' polyf = nil if (theptheme.getsrcname.asstring.right(4) = ".shp") then pname = theptheme.getsrcname.asstring.astokens(".").get(0)+"#" polyf = pftab.findfield(pname) end if (polyf = nil) then pname = theptheme.getsrcname.asstring.astokens(".").get(0)+"_" polyf = pftab.findfield(pname) end if (polyf = nil) then pname = theptheme.getsrcname.asstring+"#" polyf = pftab.findfield(pname) end if (polyf = nil) then pname = theptheme.getsrcname.asstring+"_" polyf = pftab.findfield(pname) end if (polyf = nil) then msgbox.error("Can't find polygon number field in polygon theme","BALANCE") exit end ' '----------------- '--- Calculate --- '----------------- ' '--- Initial set up --- ' 'make line attribute table editable ' lftab.seteditable(true) ' 'make index for polygon attribute table ' polydict = dictionary.make(pftab.getnumrecords * 2) for each prec in pftab poly = pftab.returnvalue(polyf, prec) polydict.add(poly, prec.asstring.asnumber) end ' '--- Loop --- ' for each lrec in lftab ' 'get line attributes ' lp = lftab.returnvalue(lpf, lrec) rp = lftab.returnvalue(rpf, lrec) lshape = lftab.returnvalue(lshapef, lrec) e = lftab.returnvalue(ef, lrec) if (mychoice = "Area Field, a") then a = lftab.returnvalue(af, lrec) end if (mychoice = "Depth Field, h and length") then h = lftab.returnvalue(hf, lrec) pointlist = lshape.aslist length = 0 for each twopoints in pointlist frompoint = twopoints.get(0) topoint = twopoints.get(1) thislength = frompoint.distance(topoint) length = length + thislength end a = length * h end ' 'calculate mixing length ' if ((lp > 1) and (rp > 1)) then lpshape = pftab.returnvalue(pshapef, polydict.get(lp)) lpcentroid = lpshape.returncenter rpshape = pftab.returnvalue(pshapef, polydict.get(rp)) rpcentroid = rpshape.returncenter dx = lpcentroid.distance(rpcentroid) end if ((rp = 1) or (lp = 1)) then pointlist = lshape.asmultipoint.aslist fulllength = 0 i = 1 while (i < pointlist.count) fx = pointlist.get(i - 1).getx fy = pointlist.get(i - 1).gety tx = pointlist.get(i).getx ty = pointlist.get(i).gety length = (((fx - tx)^2) + ((fy - ty)^2)^0.5) fulllength = fulllength + length i = i + 1 end partlength = 0 i = 1 centerfound = false while (i < pointlist.count) fx = pointlist.get(i - 1).getx fy = pointlist.get(i - 1).gety tx = pointlist.get(i).getx ty = pointlist.get(i).gety length = (((fx - tx)^2) + ((fy - ty)^2)^0.5) partlength = partlength + length if ((partlength >= (fulllength * 0.5)) and (not centerfound)) then fraction = ((fulllength * 0.5) - partlength + length) / length cx = fx + ((tx - fx) * fraction) cy = fy + ((ty - fy) * fraction) centerpoint = point.make(cx,cy) centerfound = true end i = i + 1 end end if (rp = 1) then if (polydict.get(lp) = nil) then msgbox.info(lp.asstring, "DEBUG") exit end lpshape = pftab.returnvalue(pshapef, polydict.get(lp)) lpcentroid = lpshape.returncenter dx = lpcentroid.distance(centerpoint) * 2 end if (lp = 1) then rpshape = pftab.returnvalue(pshapef, polydict.get(rp)) rpcentroid = rpshape.returncenter dx = rpcentroid.distance(centerpoint) * 2 end ' 'calculate ep ' ep = (e * a) / dx ' 'write ep to line attribute table ' lftab.setvalue(epf, lrec, ep) ' end ' 'make line attribute table non editable ' lftab.seteditable(false) ' 'final message to user ' message = "Bulk Dispersion Coefficients, ep calculated" msgbox.info(message,"BALANCE") ' '----------- '--- End --- '----------- '