@Override public void run(){
  amIActive=true;
  String demHeader=null;
  String upslopeAreaCreekHeader=null;
  String outputHeader=null;
  int numRows;
  int numCols;
  double elevation, elevationNeighbour;
  int x, y;
  double slope, maxSlope;
  int flowDir;
  int i;
  List<StreamFlow> streamFlowList=new ArrayList<>();
  List<StreamFlow> copyStreamFlowList;
  StreamFlow streamFlow2;
  List<Double> upstreamValues=new ArrayList<>();
  float progress=0;
  if (args.length <= 0) {
    showFeedback("Plugin parameters have not been set.");
    return;
  }
  for (i=0; i < args.length; i++) {
    if (i == 0) {
      demHeader=args[i];
    }
 else     if (i == 1) {
      upslopeAreaCreekHeader=args[i];
    }
 else     if (i == 2) {
      outputHeader=args[i];
    }
  }
  if ((demHeader == null) || (upslopeAreaCreekHeader == null) || (outputHeader == null)) {
    showFeedback("One or more of the input parameters have not been set properly.");
    return;
  }
  try {
    dem=new WhiteboxRaster(demHeader,"r");
    upslopeAreaCreek=new WhiteboxRaster(upslopeAreaCreekHeader,"r");
    numRows=dem.getNumberRows();
    numCols=dem.getNumberColumns();
    gridRes=dem.getCellSizeX();
    medianUpstreamArea=new WhiteboxRaster(outputHeader,"rw",demHeader,WhiteboxRaster.DataType.FLOAT,0);
    medianUpstreamArea.setPreferredPalette("blueyellow.pal");
    medianUpstreamArea.setDataScale(WhiteboxRasterBase.DataScale.CONTINUOUS);
    medianUpstreamArea.setZUnits("dimensionless");
    tmpDirectUpstreamCreekCellCount=new WhiteboxRaster(outputHeader.replace(".dep","_tmp1.dep"),"rw",demHeader,WhiteboxRaster.DataType.FLOAT,0);
    tmpDirectUpstreamCreekCellCount.isTemporaryFile=true;
    updateProgress("Loop 1 of 3:",0);
    for (int row=0; row < numRows; row++) {
      for (int col=0; col < numCols; col++) {
        if (upslopeAreaCreek.getValue(row,col) == upslopeAreaCreek.getNoDataValue()) {
          medianUpstreamArea.setValue(row,col,upslopeAreaCreek.getNoDataValue());
        }
      }
      if (cancelOp) {
        cancelOperation();
        return;
      }
      progress=(float)(100f * row / (numRows - 1));
      updateProgress("Loop 1 of 3:",(int)progress);
    }
    updateProgress("Loop 2 of 3:",0);
    for (int row=0; row < numRows; row++) {
      for (int col=0; col < numCols; col++) {
        maxSlope=Double.MIN_VALUE;
        flowDir=-1;
        if (upslopeAreaCreek.getValue(row,col) > 0) {
          elevation=dem.getValue(row,col);
          for (int c=0; c < 8; c++) {
            x=col + xd[c];
            y=row + yd[c];
            elevationNeighbour=dem.getValue(y,x);
            if (upslopeAreaCreek.getValue(y,x) > 0 && elevationNeighbour < elevation) {
              slope=(elevation - elevationNeighbour) / dd[c];
              if (slope > maxSlope) {
                maxSlope=slope;
                flowDir=c;
              }
            }
          }
          for (int c=0; c < 8; c++) {
            if (c == flowDir) {
              x=col + xd[c];
              y=row + yd[c];
              tmpDirectUpstreamCreekCellCount.incrementValue(y,x,1);
              streamFlowList.add(new StreamFlow(col,row,x,y,elevation));
            }
          }
        }
      }
      if (cancelOp) {
        cancelOperation();
        return;
      }
      progress=(float)(100f * row / (numRows - 1));
      updateProgress("Loop 2 of 3:",(int)progress);
    }
    streamFlowList=OrderStreamFlowList(streamFlowList);
    copyStreamFlowList=new ArrayList<>(streamFlowList.subList(0,streamFlowList.size()));
    updateProgress("Loop 3 of 3:",0);
    for (    StreamFlow streamFlow : streamFlowList) {
      i=streamFlowList.indexOf(streamFlow);
      if (medianUpstreamArea.getValue(streamFlow.GetToY(),streamFlow.GetToX()) == 0) {
        upstreamValues=new ArrayList<>();
        FilterStreamFlowList(copyStreamFlowList,streamFlow);
        MakeUpstreamList(copyStreamFlowList,streamFlow.GetToX(),streamFlow.GetToY(),upstreamValues);
        Collections.sort(upstreamValues);
        medianUpstreamArea.setValue(streamFlow.GetToY(),streamFlow.GetToX(),GetMedian(upstreamValues));
        streamFlow2=streamFlow;
        while ((tmpDirectUpstreamCreekCellCount.getValue(streamFlow2.GetToY(),streamFlow2.GetToX()) == 1) & (upstreamValues.size() > 2)) {
          upstreamValues.remove(upslopeAreaCreek.getValue(streamFlow2.GetToY(),streamFlow2.GetToX()));
          medianUpstreamArea.setValue(streamFlow2.GetFromY(),streamFlow2.GetFromX(),GetMedian(upstreamValues));
          x=streamFlow2.GetFromX();
          y=streamFlow2.GetFromY();
          for (          StreamFlow tempStreamFlow : copyStreamFlowList) {
            if (tempStreamFlow.GetToX() == x && tempStreamFlow.GetToY() == y) {
              streamFlow2=tempStreamFlow;
              break;
            }
          }
        }
        if (tmpDirectUpstreamCreekCellCount.getValue(streamFlow2.GetFromY(),streamFlow2.GetFromX()) == 0) {
          x=streamFlow2.GetFromX();
          y=streamFlow2.GetFromY();
          medianUpstreamArea.setValue(y,x,upslopeAreaCreek.getValue(y,x));
        }
      }
      if (cancelOp) {
        cancelOperation();
        return;
      }
      progress=(float)(100f * (i + 1) / streamFlowList.size());
      updateProgress("Loop 3 of 3:",(int)progress);
    }
    medianUpstreamArea.addMetadataEntry("Created by the " + getDescriptiveName() + " tool.");
    medianUpstreamArea.addMetadataEntry("Created on " + new Date());
    dem.close();
    upslopeAreaCreek.close();
    medianUpstreamArea.close();
    tmpDirectUpstreamCreekCellCount.close();
    returnData(outputHeader);
  }
 catch (  Exception e) {
    showFeedback(e.getMessage());
  }
 finally {
    updateProgress("Progress: ",0);
    amIActive=false;
    myHost.pluginComplete();
  }
}

@ Override public void run ( ) { amIActive = true ; String demHeader = null ; String upslopeAreaCreekHeader = null ; String outputHeader = null ; int numRows ; int numCols ; double elevation , elevationNeighbour ; int x , y ; double slope , maxSlope ; int flowDir ; int i ; List < StreamFlow > streamFlowList = new ArrayList < > ( ) ; List < StreamFlow > copyStreamFlowList ; StreamFlow streamFlow2 ; List < Double > upstreamValues = new ArrayList < > ( ) ; float progress = 0 ; if ( args . length <= 0 ) { showFeedback ( "Plugin parameters have not been set." ) ; return ; } for ( i = 0 ; i < args . length ; i ++ ) { if ( i == 0 ) { demHeader = args [ i ] ; } else if ( i == 1 ) { upslopeAreaCreekHeader = args [ i ] ; } else if ( i == 2 ) { outputHeader = args [ i ] ; } } if ( ( demHeader == null ) || ( upslopeAreaCreekHeader == null ) || ( outputHeader == null ) ) { showFeedback ( "One or more of the input parameters have not been set properly." ) ; return ; } try { dem = new WhiteboxRaster ( demHeader , "r" ) ; upslopeAreaCreek = new WhiteboxRaster ( upslopeAreaCreekHeader , "r" ) ; numRows = dem . getNumberRows ( ) ; numCols = dem . getNumberColumns ( ) ; gridRes = dem . getCellSizeX ( ) ; medianUpstreamArea = new WhiteboxRaster ( outputHeader , "rw" , demHeader , WhiteboxRaster . DataType . FLOAT , 0 ) ; medianUpstreamArea . setPreferredPalette ( "blueyellow.pal" ) ; medianUpstreamArea . setDataScale ( WhiteboxRasterBase . DataScale . CONTINUOUS ) ; medianUpstreamArea . setZUnits ( "dimensionless" ) ; tmpDirectUpstreamCreekCellCount = new WhiteboxRaster ( outputHeader . replace ( ".dep" , "_tmp1.dep" ) , "rw" , demHeader , WhiteboxRaster . DataType . FLOAT , 0 ) ; tmpDirectUpstreamCreekCellCount . isTemporaryFile = true ; updateProgress ( "Loop 1 of 3:" , 0 ) ; for ( int row = 0 ; row < numRows ; row ++ ) { for ( int col = 0 ; col < numCols ; col ++ ) { if ( upslopeAreaCreek . getValue ( row , col ) == upslopeAreaCreek . getNoDataValue ( ) ) { medianUpstreamArea . setValue ( row , col , upslopeAreaCreek . getNoDataValue ( ) ) ; } } if ( cancelOp ) { cancelOperation ( ) ; return ; } progress = ( float ) ( 100f * row / ( numRows - 1 ) ) ; updateProgress ( "Loop 1 of 3:" , ( int ) progress ) ; } updateProgress ( "Loop 2 of 3:" , 0 ) ; for ( int row = 0 ; row < numRows ; row ++ ) { for ( int col = 0 ; col < numCols ; col ++ ) { maxSlope = Double . MIN_VALUE ; flowDir = - 1 ; if ( upslopeAreaCreek . getValue ( row , col ) > 0 ) { elevation = dem . getValue ( row , col ) ; for ( int c = 0 ; c < 8 ; c ++ ) { x = col + xd [ c ] ; y = row + yd [ c ] ; elevationNeighbour = dem . getValue ( y , x ) ; if ( upslopeAreaCreek . getValue ( y , x ) > 0 && elevationNeighbour < elevation ) { slope = ( elevation - elevationNeighbour ) / dd [ c ] ; if ( slope > maxSlope ) { maxSlope = slope ; flowDir = c ; } } } for ( int c = 0 ; c < 8 ; c ++ ) { if ( c == flowDir ) { x = col + xd [ c ] ; y = row + yd [ c ] ; tmpDirectUpstreamCreekCellCount . incrementValue ( y , x , 1 ) ; streamFlowList . add ( new StreamFlow ( col , row , x , y , elevation ) ) ; } } } } if ( cancelOp ) { cancelOperation ( ) ; return ; } progress = ( float ) ( 100f * row / ( numRows - 1 ) ) ; updateProgress ( "Loop 2 of 3:" , ( int ) progress ) ; } streamFlowList = OrderStreamFlowList ( streamFlowList ) ; copyStreamFlowList = new ArrayList < > ( streamFlowList . subList ( 0 , streamFlowList . size ( ) ) ) ; updateProgress ( "Loop 3 of 3:" , 0 ) ; for ( StreamFlow streamFlow : streamFlowList ) { i = streamFlowList . indexOf ( streamFlow ) ; if ( medianUpstreamArea . getValue ( streamFlow . GetToY ( ) , streamFlow . GetToX ( ) ) == 0 ) { upstreamValues = new ArrayList < > ( ) ; FilterStreamFlowList ( copyStreamFlowList , streamFlow ) ; MakeUpstreamList ( copyStreamFlowList , streamFlow . GetToX ( ) , streamFlow . GetToY ( ) , upstreamValues ) ; Collections . sort ( upstreamValues ) ; medianUpstreamArea . setValue ( streamFlow . GetToY ( ) , streamFlow . GetToX ( ) , GetMedian ( upstreamValues ) ) ; streamFlow2 = streamFlow ; while ( ( tmpDirectUpstreamCreekCellCount . getValue ( streamFlow2 . GetToY ( ) , streamFlow2 . GetToX ( ) ) == 1 ) & ( upstreamValues . size ( ) > 2 ) ) { upstreamValues . remove ( upslopeAreaCreek . getValue ( streamFlow2 . GetToY ( ) , streamFlow2 . GetToX ( ) ) ) ; medianUpstreamArea . setValue ( streamFlow2 . GetFromY ( ) , streamFlow2 . GetFromX ( ) , GetMedian ( upstreamValues ) ) ; x = streamFlow2 . GetFromX ( ) ; y = streamFlow2 . GetFromY ( ) ; for ( StreamFlow tempStreamFlow : copyStreamFlowList ) { if ( tempStreamFlow . GetToX ( ) == x && tempStreamFlow . GetToY ( ) == y ) { streamFlow2 = tempStreamFlow ; break ; } } } if ( tmpDirectUpstreamCreekCellCount . getValue ( streamFlow2 . GetFromY ( ) , streamFlow2 . GetFromX ( ) ) == 0 ) { x = streamFlow2 . GetFromX ( ) ; y = streamFlow2 . GetFromY ( ) ; medianUpstreamArea . setValue ( y , x , upslopeAreaCreek . getValue ( y , x ) ) ; } } if ( cancelOp ) { cancelOperation ( ) ; return ; } progress = ( float ) ( 100f * ( i + 1 ) / streamFlowList . size ( ) ) ; updateProgress ( "Loop 3 of 3:" , ( int ) progress ) ; } medianUpstreamArea . addMetadataEntry ( "Created by the " + getDescriptiveName ( ) + " tool." ) ; medianUpstreamArea . addMetadataEntry ( "Created on " + new Date ( ) ) ; dem . close ( ) ; upslopeAreaCreek . close ( ) ; medianUpstreamArea . close ( ) ; tmpDirectUpstreamCreekCellCount . close ( ) ; returnData ( outputHeader ) ; } catch ( Exception e ) { showFeedback ( e . getMessage ( ) ) ; } finally { updateProgress ( "Progress: " , 0 ) ; amIActive = false ; myHost . pluginComplete ( ) ; } }
used to execute this plugin tool .
