import java.io.*; class Aslop { // Copyleft Harvey Greenberg, UW hgreen@u.washington.edu Fri Jan 24 09:49 1997 // This loads an ARC/INFO AAT file into an array of objects (see Arc.java), // processes them, and writes them back out. // Before invoking this class, we have built (in ARCINFO) a river network, // coded the shreve streamorder, and intersected the network with contour lines. // We have determined the to- and from-elevations of most river arcs, but we need // to interpolate and extrapolate to estimate the elevations of (often multiple) // stream junctions. // The first argument is the name of the stream cover. // Optional second argument = the contour interval, used to constrain extrapolation. // The default is 40. // The otional third argument is the z factor related z units to xy units. // The default is 0.3048, for xy meters and z feet. // The bytelength of the record is hardcoded; this relates to limitations in Arc.java public static void main (String args[]) throws IOException { int maxid; float zfactor = 0.3048f; float totlength,theslope,deltaz,interval = 40, tmp1,tmp2,tmp3; double lengthmax; int stringnum,nleft,nup,shrevemax,bestbranch,n; int arcstring[] = new int[12]; int upid[][] = new int[12][5]; int nbranches[] = new int[12]; long fsize; int reclength = 74, remainder; String covername; Float ob_interval,ob_zfactor; System.out.println("starting calculation of stream slopes, version 2.1"); if (args.length < 1){ System.out.println("The cover name is a required argument."); return; } else covername = args[0]; if (args.length > 1){ ob_interval = new Float(args[1]); interval = ob_interval.floatValue(); } if (args.length > 2){ ob_zfactor = new Float(args[2]); zfactor = ob_zfactor.floatValue(); } System.out.println("Cover: " + covername + ", contour interval: " + interval + ", zfactor: " + zfactor); File f = new File (covername + "/aat.adf"); DataInputStream s = new DataInputStream(new FileInputStream(f)); DataOutputStream t = new DataOutputStream(new FileOutputStream(f)); fsize = f.length(); maxid = (int) fsize / reclength; System.out.println("File size is " + fsize + " bytes, the max id is " + maxid); Arc arcs[] = new Arc[maxid]; if ((fsize % reclength) != 0){ System.out.println("The file size is not a multiple of the expected record length!\nABORTING"); } for(n = 0;n < maxid;n++) { arcs[n] = new Arc (); arcs[n].readArc(s); if(arcs[n].elvf >= 0 && arcs[n].elvt >= 0){ arcs[n].dz = arcs[n].elvf - arcs[n].elvt; arcs[n].slope = (float) arcs[n].dz / (float) arcs[n].length * zfactor; } } // for s.close(); for(int hugeit = 1;hugeit < 5;hugeit++){// It drops out after the first iteration // for medium complex topology between // contours. I have never seen it go // beyond 2 iterations. System.out.println("starting huge iteration " + hugeit + "\n"); nleft = 0; bestbranch = 0; stringnum = 1; theslope = 0; // not really needed, but prevents warning message for(int i = 0;i < maxid;i++){ if((arcs[i].elvt >= 0 || hugeit > 1) && arcs[i].elvf < 0){ // trace upstream to known elevation nleft++; deltaz = -99; totlength = (float) arcs[i].length; stringnum = 1; arcstring[1] = i; // for(stringnum = 2;stringnum <= 20;stringnum++){ // search up to next contour while(true){ // search up to next contour nup = 0; shrevemax = 0; lengthmax = 0; for(int j = 0;j< maxid;j++){ if(arcs[j].tnode == arcs[arcstring[stringnum]].fnode){ // found upstream arc nup++; upid[stringnum][nup] = j; // remember all branches if(arcs[j].shreve > shrevemax){ shrevemax = arcs[j].shreve; bestbranch = j; } else if(arcs[j].shreve == shrevemax && arcs[j].length > lengthmax){ lengthmax = arcs[j].length; bestbranch = j; } } // found upstream arc } // looking for upstream arcs nbranches[stringnum] = nup; if(nup == 0) break; //dangling source stringnum++; totlength += arcs[bestbranch].length; arcstring[stringnum] = bestbranch; if(arcs[bestbranch].elvf >= 0) break; // found upstream contour if(stringnum >= 20){ // we should have broken out of the loop System.out.println("Error tracing from arc " + i); return; } } // search up to next contour if(stringnum >= 20){ // we should have broken out of the loop System.out.println("Error tracing from arc " + i); return; } theslope = -9; if(arcs[arcstring[stringnum]].elvf < 0){ // dangling source for(int j = 0;j< maxid;j++){ if(arcs[j].fnode == arcs[i].tnode && arcs[j].slope > 0.0){ deltaz = arcs[j].slope * totlength / zfactor; if(deltaz > interval) deltaz = interval * 0.8f; break; } } } //dangling source else if(arcs[i].elvt < 0){ // dangling mouth for(int j = 0;j< maxid;j++){ if(arcs[j].tnode == arcs[arcstring[stringnum]].fnode && arcs[j].slope > 0.0){ deltaz = arcs[j].slope * totlength / zfactor; if(deltaz > interval) deltaz = interval * 0.8f; arcs[i].elvt =arcs[arcstring[stringnum]].elvf - deltaz; break; // this would not be a junction } } } //dangling mouth else deltaz = arcs[arcstring[stringnum]].elvf - arcs[i].elvt; if(deltaz < 0) continue; // dangling source or mouth to save for next theslope = deltaz / totlength * zfactor; for(int sn = 1;sn <= stringnum;sn++){ // go up the chain, fixing if(arcs[arcstring[sn]].elvf < 0){ arcs[arcstring[sn]].elvf = arcs[arcstring[sn]].elvt + theslope * (float)arcs[arcstring[sn]].length / zfactor; //nleft--; } arcs[arcstring[sn]].dz = arcs[arcstring[sn]].elvf - arcs[arcstring[sn]].elvt; arcs[arcstring[sn]].slope = theslope; if(sn < stringnum){ for(int k = 1;k <= nbranches[sn];k++){ //if(elvt[upid[sn][k]] < 0) nleft--; arcs[upid[sn][k]].elvt = arcs[arcstring[sn]].elvf; if(arcs[upid[sn][k]].elvf >= 0) { // Finish the arc. 1/6/97 arcs[upid[sn][k]].dz = arcs[upid[sn][k]].elvf - arcs[upid[sn][k]].elvt; arcs[upid[sn][k]].slope = (float) arcs[upid[sn][k]].dz / (float) arcs[upid[sn][k]].length * zfactor; } } // for each upstream branch } } } // this arc needs work else if (arcs[i].dz == 0 && arcs[i].elvt >= 0 && arcs[i].elvf >= 0){ //just needs a little work arcs[i].dz = arcs[i].elvf - arcs[i].elvt; arcs[i].slope = (float) arcs[i].dz / (float) arcs[i].length * zfactor; } } // for each arc System.out.println("finished huge iteration " + hugeit + "," + nleft + " more arcs to do\n"); if(nleft == 0) break; // we waste a little time by not decrementing nleft when we fix an arc } // huge iteration System.out.println("Writing to to the AAT file."); for(int i = 0;i < maxid;i++){ arcs[i].writeArc(t); } t.close(); } }