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

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

/**
 * Extract watershed
 *
 * @author JK (using OD's template)
 */
@Name("gage_watershed")
@Description("Example of gage watershed")
@Path("m/extract_watershed/1.0")
//@Resource(file = "mpirun", type = REFERENCE, id = "mpirun")
@Resource(file = "gdal_polygonize.py", type = REFERENCE, id = "gdal_polygonize")
@Resource(file = "gdal_calc.py", type = REFERENCE, id = "gdal_calc")
@Resource(file = "ogr2ogr", type = REFERENCE, id = "ogr")
@Resource(file = "gdalwarp", type = REFERENCE, id = "gdalwarp")
@Resource(file = "/bin/lin-amd64/moveoutletstostrm", type = EXECUTABLE, id = "moveoutletstostrm")
@Resource(file = "/bin/lin-amd64/gagewatershed", type = EXECUTABLE, id = "gagewatershed")

public class V1_0 extends ModelDataService {

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

        String file1 = parameter().getString("outlet");
        File file_1 = attachments().getFile(file1);
        String input_outlet_path = file_1.getPath();
        String bound_wkt = parameter().getString("bound_wkt");

        String bound_arr = bound_wkt.substring(bound_wkt.indexOf("(") + 2, bound_wkt.indexOf(")"));
        String[] bound_arr_2 = bound_arr.split(",");
        float[] bound_x = new float[bound_arr_2.length];
        float[] bound_y = new float[bound_arr_2.length];

        for (int i = 0; i < bound_arr_2.length; i++) {

            String[] bound_arr_3 = bound_arr_2[i].split(" ");
            LOG.info(bound_arr_3[0]);
            bound_x[i] = Float.parseFloat(bound_arr_3[0]);

            bound_y[i] = Float.parseFloat(bound_arr_3[1]);
        }

        Arrays.sort(bound_x);
        Arrays.sort(bound_y);
        float x_min = bound_x[0];
        float x_max = bound_x[bound_x.length - 1];
        float y_min = bound_y[0];
        float y_max = bound_y[bound_y.length - 1];

        String bound_4326 = parameter().getString("bound_wkt_2");
        String[] coord_2 = bound_4326.split(" ");
        String[] xy_4326_1 = coord_2[1].split(",");
        String x_min_1 = xy_4326_1[1];
        String y_min_1 = xy_4326_1[0];
        String[] xy_4326_2 = coord_2[2].split(",");
        String x_max_1 = xy_4326_2[1];
        String y_max_1 = xy_4326_2[0];

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

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

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

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

        e = resources().getExe("gdal_calc");
        e.addArguments("-A", wd + "/NHD_dir.tif", "--outfile", wd + "/flow_dir.tif", "--overwrite", "--calc", "1*(A==1) | 2*(A==128) | 3*(A==64) | 4*(A==32) | 5*(A==16) | 6*(A==8) | 7*(A==4) | 8*(A==2)");
        e.exec();

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

        //e = resources().getExe("mpirun");
        //e2 = resources().getFile("gagewatershed");
        e = resources().getExe("gagewatershed");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-p", wd+ "/flow_dir.tif", "-o", wd + "/outlet_moved.shp", "-gw", wd+"/gage_watershed.tif");        
        e.addArguments("-p", wd + "/flow_dir.tif", "-o", wd + "/outlet_moved.shp", "-gw", wd + "/gage_watershed.tif");
        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("-s_srs", "EPSG:5070", "-t_srs", "EPSG:4326", wd + "/watershed.shp", wd + "/b_watershed.shp", "-clipdst", x_min_1, y_min_1, x_max_1, y_max_1);
        e.exec();
    }

    // 3) provide the temperature as a result.
    @Override
    protected void postProcess() throws Exception {
        File ws = workspace().getDir();
        results().put(new File(ws, "gage_watershed.tif"), "gauged watershed");
        results().put(new File(ws, "watershed.dbf"), "total watershed");
        results().put(new File(ws, "watershed.shx"), "total watershed");
        results().put(new File(ws, "watershed.shp"), "total watershed");
        results().put(new File(ws, "outlet_moved.dbf"), "outlet moved");
        results().put(new File(ws, "outlet_moved.shx"), "outlet moved");
        results().put(new File(ws, "outlet_moved.shp"), "outlet moved");

    }

}