'
'----------------------------
'--- Creation information ---
'----------------------------
'
'Name: pickload.ave
'Version: 1.0
'Date: 02/11/97
'Author: Ferdi Hellweger
'        Center for Research in Water Resources
'        The University of Texas at Austin
'        ferdi@crwr.utexas.edu
'
'---------------------------
'--- Purpose/Description ---
'---------------------------
'
'This program picks up the nonpoint source load for each polygon from
'a nonpoint source load (weighted flow accumulation) grid with the
'cellvalue request. The nonpoint source load is written to the wnp
'field in the polygon attribute table. 
'
'---------------------------------------------
'--- 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
vgraphics = theview.getgraphics
'
'------------------
'--- 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
    gfound = false
    pfound = false
    for each activetheme in theactivethemes
        if (activetheme.getclass.getclassname = "gtheme") then
            thegtheme = activetheme
            gfound = true
        end
        if (activetheme.getclass.getclassname = "ftheme") then
            theptheme = activetheme
            pfound = true
        end
    end
end
if (not gfound) then
    msgbox.error("One theme needs to be a grid theme", "BALANCE")
    exit
end
if (not pfound) then
    msgbox.error("One theme needs to be a polygon theme", "BALANCE")
    exit
end
'
'---------------------
'--- Set up themes ---
'---------------------
'
'grid theme
'
thegrid = thegtheme.getgrid
'
'check below causes error crash for some reason
'
'if (thegrid = nil) then
'    msgbox.error("Can't open grid theme","BALANCE")
'    exit 
'end
thecellsize = thegrid.getcellsize
'
'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
'
wnpf = pftab.findfield("wnp")
if (wnpf = nil) then
    addfield = msgbox.yesno("Can't find 'wnp' field in polygon theme.  Add it?","BALANCE", true)
    if (addfield) then
        pftab.seteditable(true)
        wnpf = field.make("wnp", #FIELD_DECIMAL, 16, 4)
        pftab.addfields({wnpf})
        pftab.seteditable(false)
    else
        exit
    end
end
'
'-----------------
'--- Calculate ---
'-----------------
'
'Note that due to instabilities in the flowaccumulation algorithm
'at sinks the wnp load can not be simply picked of the grid at the
'centroid (the sink).  The wnp load is the integral of the grid
'values on a 5x5 box around the centroid.  This assumes that the
'flow is directly to the centroid and not around the perimeter of
'the box.  Observations support this assumption.  The flow direction
'grid could be used to check and/or modify the algorithm.
'
'--- Initial set up ---
'
'make polygon attribute table editable
'
pftab.seteditable(true)
'
'--- Loop ---
'
for each prec in pftab
    '
    'get centroid
    '
    pshape = pftab.returnvalue(pshapef, prec)
    cenp = pshape.returncenter
    cenx = cenp.getx
    ceny = cenp.gety
    '
    'plot centroid
    '
    cengs = graphicshape.make(cenp)
    vgraphics.add(cengs)
    '
    'check if centroid is in polygon
    '
    inside = cenp.iscontainedin(pshape)
    if (not inside) then
        msgbox.error("Polygon centroid not inside polygon", "BALANCE")
    end
    '
    'get wnp
    '
    wnp = 0
    '
    pickx = cenx + (0 * thecellsize)
    picky = ceny + (2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (1 * thecellsize)
    picky = ceny + (2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (2 * thecellsize)
    picky = ceny + (2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (2 * thecellsize)
    picky = ceny + (1 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (2 * thecellsize)
    picky = ceny + (0 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (2 * thecellsize)
    picky = ceny + (-1 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (2 * thecellsize)
    picky = ceny + (-2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (1 * thecellsize)
    picky = ceny + (-2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (0 * thecellsize)
    picky = ceny + (-2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (-1 * thecellsize)
    picky = ceny + (-2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (-2 * thecellsize)
    picky = ceny + (-2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (-2 * thecellsize)
    picky = ceny + (-1 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (-2 * thecellsize)
    picky = ceny + (0 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (-2 * thecellsize)
    picky = ceny + (1 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (-2 * thecellsize)
    picky = ceny + (2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    pickx = cenx + (-1 * thecellsize)
    picky = ceny + (2 * thecellsize)
    pickp = point.make(pickx, picky)
    wnpi = thegtheme.returncellvalue(pickp)
    wnp = wnp + wnpi.clone
    '
    'write wnp to polygon attribute table
    '
    pftab.setvalue(wnpf, prec, wnp)
    '
end
'
'make polygon attribute table non editable
'
pftab.seteditable(false)
'
'final message to user
'
message = "Nonpoint Source Load, wnp picked"
msgbox.info(message,"BALANCE")
'
'-----------
'--- End ---
'-----------
'