
FIG=true
FIG=false
include("/home/ezaron/opt/julia/edzlib.jl")
if (FIG)
    using Colors
end

# From load_balance.jl:
nodes=128
ppn=20
cmap1 = Colors.distinguishable_colors(nodes)

fid = open("./badfiles.txt","w")

#for whichcheck=collect(1:3)
#for whichcheck=2
#for whichcheck=4
for whichcheck=1

figure(width=1000,height=700)
count = 0
p = 0
workdir = "/scratch/ezaron/workdir12.2/"
#workdir = "/scratch/ezaron/workdir/"
for mynode=1:nodes
    mydir = workdir * @sprintf("n%04i",mynode)
    dlist = readdir(mydir)
    for dir=dlist
        println("Working on dir = ",dir)
        jdir = mydir * "/" * dir
        println("jdir = ",jdir)
        jlist = readdir(jdir)
        for jnum=jlist
            myj = jdir * "/" * jnum
            println("myj = ",myj)
            fin = myj * "/lonlatL.nc"
            lon0 = ncread(fin,"lon0")[1]
            lat0 = ncread(fin,"lat0")[1]
            count = count + 1
            if (count == 1)
                p=plot([lon0],[lat0],"k.")
                xlim(-3,363)
                ylim(-70,70)
            end
            oplot([lon0],[lat0],"k.")
            if (1 == whichcheck)
                fin = myj * "/UV.nc"
                if (!isfile(fin))
                    oplot([lon0],[lat0],"ro",markersize=0.175)
                    continue
                end
                LON = ncvarget(fin,"LON")
                if (length(LON) == 0)
                    oplot([lon0],[lat0],"rx",markersize=0.175)
                    continue
                end
                if  (maximum(LON) > 1e33)
                    oplot([lon0],[lat0],"rx",markersize=0.5)
                    # CLEAN UP SOME BAD FILES?
                    run(`rm $fin`)
                    continue
                end
            end
            if (2 == whichcheck)
                fin = myj * "/c2a.nc"
                if (!isfile(fin))
                    oplot([lon0],[lat0],"ro",markersize=0.125)
                end
                fin = myj * "/j1c.nc"
                if (!isfile(fin))
                    oplot([lon0],[lat0],"ro",markersize=0.25)
                end
                fin = myj * "/j2c.nc"
                if (!isfile(fin))
                    oplot([lon0],[lat0],"ro",markersize=0.375)
                end
                fin = myj * "/j2d.nc"
                if (!isfile(fin))
                    oplot([lon0],[lat0],"ro",markersize=0.5)
                end
                fin = myj * "/sab.nc"
                if (!isfile(fin))
                    oplot([lon0],[lat0],"ro",markersize=0.625)
                end
                if (isfile(fin))
                    lat = ncvarget(fin,"lat")
                    sla = ncvarget(fin,"sla")
                    ind1 = find(isnan,lat)
                    ind2 = find(isnan,sla)
                    if ( length(ind1) != length(ind2) )
#                        oplot([lon0],[lat0],"ro",markersize=0.75)
                        stophere
                    end
                end
            end
           if (3 == whichcheck)
               fin = myj * "/HA.nc"
                if (!isfile(fin))
                    oplot([lon0],[lat0],"ro")
                end
                lon = ncvarget(fin,"lon")
                lat = ncvarget(fin,"lat")
                ind1 = find(isnan,lon)
                ind2 = find(isnan,lat)
                if ( length(ind1) != length(ind2) )
                    oplot([lon0],[lat0],"ro",markersize=0.5)
                end
           end 
           if (4 == whichcheck)
                fin = myj * "/XoutK1.nc"
                if (isfile(fin))
                    oplot([lon0],[lat0],marker="filled circle",markersize=0.25)
                else
                    println(fid,myj)
                    continue
                end
               # Weird -- this intermittently throws an HDF error!:
               try
                   L=ncgetatt(fin,"Global","Ldata")
               catch
                   oplot([lon0],[lat0],"ro",markersize=0.25)
               end
            end
        end
    end
    if (mod(mynode-1,16) == 0)
        display(p)
        sleep(0.1)
    end
end

close(fid)

if (whichcheck == 1)
    title("genUV")
elseif (whichcheck == 2)
    title("genSSH")
elseif (whichcheck == 3)
    title("genHA")
elseif (whichcheck == 4)
    title("Xout.nc")
else
    stophere
end
display(p)

end

println("DONE!")
