V1_0.java [src/java/m/watershed/basic] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.watershed.basic;

import csip.api.server.Executable;
import csip.ModelDataService;
import javax.ws.rs.Path;
import csip.annotations.*;
import java.io.*;
import csip.annotations.Resource;

import static csip.annotations.ResourceType.*;

/**
 * basic
 *
 * @author JK (using OD's template)
 */
@Name("basic_tools")
@Description("Example of an Basic tool")
@Path("m/basic/1.0")
//@Resource(file = "mpirun", type = REFERENCE, id = "mpirun")
@Resource(file = "gdal_polygonize.py", type = REFERENCE, id = "gdal_polygonize")
@Resource(file = "ogr2ogr", type = REFERENCE, id = "ogr")
//@Resource(file = "/bin/lin-amd64/mod_peukerdouglas", type = REFERENCE, id = "mod_PD")
@Resource(file = "/bin/lin-amd64/mod_peukerdouglas", type = EXECUTABLE, id = "mod_peukerdouglas")
@Resource(file = "/bin/lin-amd64/pitremove", type = EXECUTABLE, id = "pitremove")
@Resource(file = "/bin/lin-amd64/d8flowdir", type = EXECUTABLE, id = "d8flowdir")
@Resource(file = "/bin/lin-amd64/aread8", type = EXECUTABLE, id = "aread8")
@Resource(file = "/bin/lin-amd64/gridnet", type = EXECUTABLE, id = "gridnet")
@Resource(file = "/bin/lin-amd64/moveoutletstostrm", type = EXECUTABLE, id = "moveoutletstostrm")
@Resource(file = "/bin/lin-amd64/dropanalysis", type = EXECUTABLE, id = "dropanalysis")
@Resource(file = "/bin/lin-amd64/threshold", type = EXECUTABLE, id = "threshold")
@Resource(file = "/bin/lin-amd64/streamnet", type = EXECUTABLE, id = "streamnet")
@Resource(file = "/bin/lin-amd64/gagewatershed", type = EXECUTABLE, id = "gagewatershed")

public class V1_0 extends ModelDataService {

    // 2) watershed
    @Override
    protected void doProcess() throws Exception {

        File ws = workspace().getDir();
        String wd = ws.toString();

        String file1 = parameter().getString("dem");
        File file_obj = attachments().getFile(file1);
        String orig_DEM_path = file_obj.getPath();
        LOG.info(orig_DEM_path);
        String file2 = parameter().getString("outlet");
        File file_outlet = attachments().getFile(file2);
        String outlet_pt_path = file_outlet.getPath();
        String thre = "";

        //Executable e = resources().getExe("mpirun");
        //File e2 = resources().getFile("pitremove");
        Executable e = resources().getExe("pitremove");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-z", orig_DEM_path, "-fel", wd + "/dem_fill.tif");        
        e.addArguments("-z", orig_DEM_path, "-fel", wd + "/dem_fill.tif");
        e.exec();

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("d8flowdir");
        e = resources().getExe("d8flowdir");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-fel", wd+"/dem_fill.tif", "-sd8", wd + "/slope.tif", "-p", wd +"/dir.tif");        
        e.addArguments("-fel", wd + "/dem_fill.tif", "-sd8", wd + "/slope.tif", "-p", wd + "/dir.tif");
        e.exec();

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("aread8");
        e = resources().getExe("aread8");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-p", wd+"/dir.tif", "-ad8", wd + "/accumulation.tif", "-nc");        
        e.addArguments("-p", wd + "/dir.tif", "-ad8", wd + "/accumulation.tif", "-nc");
        e.exec();

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("gridnet");
        e = resources().getExe("gridnet");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-p", wd+"/dir.tif", "-plen", wd +"/lngst_path.tif", "-tlen", wd +"/total_path.tif", "-gord", wd +"/gorder.tif");        
        e.addArguments("-p", wd + "/dir.tif", "-plen", wd + "/lngst_path.tif", "-tlen", wd + "/total_path.tif", "-gord", wd + "/gorder.tif");
        e.exec();

        // Do not use parallel process for mod_perukerdouglas.
        e = resources().getExe("mod_peukerdouglas");
        e.addArguments("-fel", wd + "/dem_fill.tif", "-ad8", wd + "/accumulation.tif", "-ss", wd + "/PD_stream.tif");
        e.exec();

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("moveoutletstostrm");
        e = resources().getExe("moveoutletstostrm");
        //e.addArguments("-np", "2", "-host", "localhost",e2.getAbsolutePath(), "-p", wd+"/dir.tif", "-src", wd +"/PD_stream.tif", "-o", outlet_pt_path, "-om", wd +"/outlet_moved.shp", "-md", 300);        
        e.addArguments("-p", wd + "/dir.tif", "-src", wd + "/PD_stream.tif", "-o", outlet_pt_path, "-om", wd + "/outlet_moved.shp", "-md", 300);
        e.exec();

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("aread8");
        e = resources().getExe("aread8");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-p", wd+"/dir.tif", "-o", wd +"/outlet_moved.shp", "-wg", wd+"/PD_stream.tif", "-ad8", wd +"/area_wg.tif", "-nc");        
        e.addArguments("-p", wd + "/dir.tif", "-o", wd + "/outlet_moved.shp", "-wg", wd + "/PD_stream.tif", "-ad8", wd + "/area_wg.tif", "-nc");
        e.exec();

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("dropanalysis");
        e = resources().getExe("dropanalysis");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-ad8", wd+"/accumulation.tif", "-p", wd +"/dir.tif", "-fel", wd+"/dem_fill.tif", "-ssa", wd +"/area_wg.tif", "-o", wd+"/outlet_moved.shp", "-par", 5, 1500, 15, 0, "-drp", wd+ "/drop.txt");        
        e.addArguments("-ad8", wd + "/accumulation.tif", "-p", wd + "/dir.tif", "-fel", wd + "/dem_fill.tif", "-ssa", wd + "/area_wg.tif", "-o", wd + "/outlet_moved.shp", "-par", 5, 15000, 15, 0, "-drp", wd + "/drop.txt");
        e.exec();

        BufferedReader drp = null;

        try {

            String sCurrentLine;
            String last_line = "";
            drp = new BufferedReader(new FileReader(wd + "/drop.txt"));

            while ((sCurrentLine = drp.readLine()) != null) {
                last_line = sCurrentLine;
            }
            String[] parts = last_line.split(": ");

            thre = parts[1];
            if ("0.000000".equals(thre)) {
                thre = "10";
            }
            LOG.info(thre);
        } catch (IOException exception) {
            System.err.println("Caught IOException: " + exception.getMessage());
        } finally {
            try {
                if (drp != null) {
                    drp.close();
                }
            } catch (IOException ex) {
                System.err.println("Caught IOException: " + ex.getMessage());
            }
        }

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("threshold");
        e = resources().getExe("threshold");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-ssa", wd+"/area_wg.tif", "-src", wd +"/stream.tif", "-thresh", thre);        
        e.addArguments("-ssa", wd + "/area_wg.tif", "-src", wd + "/stream.tif", "-thresh", thre);
        e.exec();

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("streamnet");
        e = resources().getExe("streamnet");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-p", wd+"/dir.tif", "-fel", wd +"/dem_fill.tif", "-ad8", wd+"/accumulation.tif", "-src", wd+"/stream.tif", "-o", wd+"/outlet_moved.shp", "-ord", wd+ "/ord.tif", "-tree", wd+ "/demtree.dat", "-coord", wd+ "/demcoord.dat", "-net", wd+ "/streamnet.shp", "-w", wd+"/demw.tif");        
        e.addArguments("-p", wd + "/dir.tif", "-fel", wd + "/dem_fill.tif", "-ad8", wd + "/accumulation.tif", "-src", wd + "/stream.tif", "-o", wd + "/outlet_moved.shp", "-ord", wd + "/ord.tif", "-tree", wd + "/demtree.dat", "-coord", wd + "/demcoord.dat", "-net", wd + "/streamnet.shp", "-w", wd + "/demw.tif");
        e.exec();

        e = resources().getExe("gdal_polygonize");
        e.addArguments(wd + "/demw.tif", "-f", "ESRI Shapefile", wd + "/subwatershed.shp");
        e.exec();

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("gagewatershed");
        e = resources().getExe("gagewatershed");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-p", wd+"/dir.tif", "-o", wd +"/outlet_moved.shp", "-gw", wd+"/gage_watershed.tif", "-id", wd+"/idfile.txt");        
        e.addArguments("-p", wd + "/dir.tif", "-o", wd + "/outlet_moved.shp", "-gw", wd + "/gage_watershed.tif", "-id", wd + "/idfile.txt");
        e.exec();

        e = resources().getExe("gdal_polygonize");
        e.addArguments(wd + "/gage_watershed.tif", "-f", "ESRI Shapefile", wd + "/b_watershed.shp");
        e.exec();

        e = resources().getExe("ogr");
        e.addArguments("-t_srs", "EPSG:4326", wd + "/watershed.shp", wd + "/b_watershed.shp");
        e.exec();
    }

    // 3) provide the result.
    @Override
    protected void postProcess() throws Exception {

        File ws = workspace().getDir();
        results().put(new File(ws, "accumulation.tif"), "Flow Accumulation");
        results().put(new File(ws, "demcoord.dat"), "channel network tree coordinates");
        results().put(new File(ws, "demtree.dat"), "list of links in channel network tree");
        results().put(new File(ws, "dem_fill.tif"), "Filled DEM");
        results().put(new File(ws, "streamnet.dbf"), "channel network");
        results().put(new File(ws, "streamnet.shx"), "channel network");
        results().put(new File(ws, "streamnet.shp"), "channel network");
        results().put(new File(ws, "demw.tif"), "subwatershed");
        results().put(new File(ws, "gage_watershed.tif"), "watershed boundary");
        results().put(new File(ws, "gorder.tif"), "Strahler order");
        results().put(new File(ws, "ord.tif"), "Strahler order for stream cell");
        results().put(new File(ws, "PD_stream.tif"), "Peuker Douglas stream cell");
        results().put(new File(ws, "lngst_path.tif"), "The longest flow path");
        results().put(new File(ws, "dir.tif"), "Eight flow direction");
        results().put(new File(ws, "slope.tif"), "eight direction slope");
        results().put(new File(ws, "stream.tif"), "thresholded stream raster");
        results().put(new File(ws, "subwatershed.dbf"), "sub watershed");
        results().put(new File(ws, "subwatershed.shx"), "sub watershed");
        results().put(new File(ws, "subwatershed.shp"), "sub watershed");
        results().put(new File(ws, "total_path.tif"), "total flow length");
        results().put(new File(ws, "area_wg.tif"), "weighted flow accumulation");
        results().put(new File(ws, "outlet_moved.dbf"), "moved outlet");
        results().put(new File(ws, "outlet_moved.shx"), "moved outlet");
        results().put(new File(ws, "outlet_moved.shp"), "moved outlet");
        results().put(new File(ws, "drop.txt"), "drop analysis table");
        results().put(new File(ws, "idfile.txt"), "id file");
        results().put(new File(ws, "watershed.dbf"), "watershed boundary");
        results().put(new File(ws, "watershed.shx"), "watershed boundary");
        results().put(new File(ws, "watershed.shp"), "watershed boundary");
    }

}