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

import csip.api.server.Executable;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.EXECUTABLE;
import static csip.annotations.ResourceType.OUTPUT;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import java.io.*;

/**
 * Grid_analysis
 *
 * @author @author HK (using JK's template (using OD's template))
 */
@Name("grid_analysis")
@Description("Example of grid analysis")
@Path("m/grid_analysis/2.0")
@Resource(file = "/bin/lin-amd64_TD537/gridnet", type = EXECUTABLE, id = "gridnet")
@Resource(file = "*.txt", type = OUTPUT)

public class V2_0 extends ModelDataService {

    static final String KEY_DIR = "flow_8_dir";

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

        String file_string = "-";

        if (attachments().hasFile(parameter().getString(KEY_DIR))) {
            file_string = parameter().getString(KEY_DIR);
            File file_obj = attachments().getFile(file_string);
            String flow_8dir_path = file_obj.getPath();
            String wd = workspace().getDir().toString();

            Executable e = resources().getExe("gridnet");
            e.addArguments("-p", flow_8dir_path, "-plen", wd + "/longest_flow_kength_up.tif", "-tlen", wd + "/total_path_length_up.tif", "-gord", wd + "/strahler_order.tif");
            StringWriter err = new StringWriter();
            e.redirectError(err);

            int ret = e.exec();

            if (ret != 0) {
                String error = err.toString();
                throw new ServiceException("Error : " + error);
            }
        } else {
            throw new ServiceException("No DIR File listet!");
        }
    }

    // 3) provide the result.
    @Override
    protected void postProcess() throws Exception {
        File ws = workspace().getDir();
        results().put(new File(ws, "longest_flow_kength_up.tif"), "Longest flow path");
        results().put(new File(ws, "total_path_length_up.tif"), "Total lengths of flow path");
        results().put(new File(ws, "strahler_order.tif"), "Grid network order");
    }
}