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

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

/**
 * Extract Stream
 *
 * @author JK (using OD's template)
 */
@Name("extract_stream")
@Description("Example of an stream and channel extraction")
@Path("m/extract_stream/1.0")
//@Resource(file = "mpirun", type = REFERENCE, id = "mpirun")
@Resource(file = "gdal_polygonize.py", type = REFERENCE, id = "gdal_polygonize")
@Resource(file = "/bin/lin-amd64/streamnet", type = EXECUTABLE, id = "streamnet")

public class V1_0 extends ModelDataService {

    // 2) convert the temperature
    @Override
    protected void doProcess() throws Exception {
        String ip = request().getRemoteAddr();

        String file1 = parameter().getString("flow_dir");
        File file_1 = attachments().getFile(file1);
        String flow_dir_path = file_1.getPath();

        String file2 = parameter().getString("filled_dem");
        File file_2 = attachments().getFile(file2);
        String filled_dem_path = file_2.getPath();

        String file3 = parameter().getString("flow_acc");
        File file_3 = attachments().getFile(file3);
        String flow_acc_path = file_3.getPath();

        String file4 = parameter().getString("src");
        File file_4 = attachments().getFile(file4);
        String input_stream_path = file_4.getPath();

        String file5 = parameter().getString("outlet");
        File file_5 = attachments().getFile(file5);
        String input_outlet_path = file_5.getPath();
        String wd = workspace().getDir().toString();

        //Executable e = resources().getExe("mpirun");
        //File e2 = resources().getFile("streamnet");
        Executable e = resources().getExe("streamnet");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-p", flow_dir_path, "-fel", filled_dem_path, "-ad8", flow_acc_path, "-src", input_stream_path, "-o", input_outlet_path, "-ord", wd+ "/ord.tif", "-tree", wd+ "/demtree.dat", "-coord", wd+ "/demcoord.dat", "-net", wd+ "/demnet.shp", "-w", wd+ "/demw.tif");        
        e.addArguments("-p", flow_dir_path, "-fel", filled_dem_path, "-ad8", flow_acc_path, "-src", input_stream_path, "-o", input_outlet_path, "-ord", wd + "/ord.tif", "-tree", wd + "/demtree.dat", "-coord", wd + "/demcoord.dat", "-net", wd + "/demnet.shp", "-w", wd + "/demw.tif");
        e.exec();

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

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

        File ws = workspace().getDir();
        results().put(new File(ws, "ord.tif"), "network order grid");
        results().put(new File(ws, "demtree.dat"), "list of links in channel network tree");
        results().put(new File(ws, "demcoord.dat"), "list of coordinates in channel network tree");
        results().put(new File(ws, "demw.tif"), "output grid of watershed identifiers");
        results().put(new File(ws, "demnet.dbf"), "channel network shapefile shp");
        results().put(new File(ws, "demnet.shx"), "channel network shapefile shx");
        results().put(new File(ws, "demnet.shp"), "channel network shapefile dbf");
        results().put(new File(ws, "sub_watershed.dbf"), "sub watershed");
        results().put(new File(ws, "sub_watershed.shx"), "sub watershed");
        results().put(new File(ws, "sub_watershed.shp"), "sub watershed");

    }

}