V1_0.java [src/java/m/watershed/slopelength] 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.slopelength;

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

/**
 * slope length
 *
 * @author JK (using OD's template)
 */
@Name("slopelength")
@Description("Example of slopelength")
@Path("m/slopelength/1.0")
@Resource(file = "/python/zonalstats.py", type = FILE, id = "zonalstats")
@Resource(file = "/python/slope_length.py", type = FILE, id = "slope_length")
@Resource(file = "/python/cutoff_px.py", type = FILE, id = "cutoff_px")
@Resource(file = "gdalwarp", type = REFERENCE, id = "gdalwarp")
@Resource(file = "gdal_translate", type = REFERENCE, id = "gdal_translate")
@Resource(file = "gdaltransform", type = REFERENCE, id = "gdaltransform")
@Resource(file = "ogr2ogr", type = REFERENCE, id = "ogr")
//@Resource(file = "mpirun", type = REFERENCE, id = "mpirun")
@Resource(file = "python", type = REFERENCE, id = "python")
@Resource(file = "gdal_calc.py", type = REFERENCE, id = "gdal_calc")
@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/gridnet", type = EXECUTABLE, id = "gridnet")

public class V1_0 extends ModelDataService {

    File result_file = null;

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

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

        String file1 = parameter().getString("boundary");
        File file_1 = attachments().getFile(file1);
        String boundary_path = file_1.getPath();

        Executable e = resources().getExe("ogr");
        e.addArguments("-t_srs", "EPSG:5070", "-s_srs", "EPSG:4326", wd + "/boundary.shp", boundary_path);
        e.exec();

        String x_min = parameter().getString("l");
        String y_min = parameter().getString("b");
        String x_max = parameter().getString("r");
        String y_max = parameter().getString("t");

        e = resources().getExe("gdalwarp");
        e.addArguments("/mnt/csip-watershed/DEM.vrt", wd + "/DEM_clip.tif", "-te", x_min, y_min, x_max, y_max, "-multi");
        e.exec();

        //e = resources().getExe("mpirun");
        //File e2 = resources().getFile("pitremove");
        e = resources().getExe("pitremove");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-z", wd+ "/DEM_clip.tif", "-fel", wd + "/dem_fill.tif");        
        e.addArguments("-z", wd + "/DEM_clip.tif", "-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 + "/sd8.tif", "-p", wd + "/p.tif");        
        e.addArguments("-fel", wd + "/dem_fill.tif", "-sd8", wd + "/sd8.tif", "-p", wd + "/p.tif");
        e.exec();

        File f_1 = resources().getFile("slope_length");
        e = resources().getExe("python");
        e.addArguments(f_1.getAbsolutePath(), wd + "/p.tif", wd + "/sd8.tif", wd + "/new_slope.tif", wd + "/dir_cut.tif");
        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_cut.tif", "-plen", wd + "/p_len.tif", "-tlen", wd + "/tlen.tif", "-gord", wd + "/gord.tif");        
        e.addArguments("-p", wd + "/dir_cut.tif", "-plen", wd + "/p_len.tif", "-tlen", wd + "/tlen.tif", "-gord", wd + "/gord.tif");
        e.exec();

        File f_2 = resources().getFile("cutoff_px");
        e = resources().getExe("python");
        e.addArguments(f_2.getAbsolutePath(), wd + "/p.tif", wd + "/sd8.tif", wd + "/p_len.tif", wd + "/slope_length.tif");
        e.exec();

        e = resources().getExe("gdal_translate");
        e.addArguments("-a_nodata", -32768, wd + "/slope_length.tif", wd + "/plen.tif");
        e.exec();

        File f_3 = resources().getFile("zonalstats");
        e = resources().getExe("python");
        e.addArguments(f_3.getAbsolutePath(), wd + "/boundary.shp", wd + "/plen.tif", -32768);
        e.exec();
    }

    // 3) provide the temperature as a result.
    @Override
    protected void postProcess() throws Exception {
        File ws = workspace().getDir();
        results().put(new File(ws, "plen.tif"), "slope length raster");
        results().put(new File(ws, "dir_cut.tif"), "cutoff direction");
        results().put(new File(ws, "results.csv"), "zontal stats results");
    }

}