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

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.*;

/**
 * cfactor
 *
 * @author JK (using OD's template)
 */
@Name("cfactor")
@Description("Example of cfactor")
@Path("m/cfactor/1.0")
@Resource(file = "/python/zonalstats.py", type = FILE, id = "zonalstats")
@Resource(file = "/python/cfactor.py", type = FILE, id = "cfactor")
@Resource(file = "gdal_translate", type = REFERENCE, id = "gdal_translate")
@Resource(file = "ogr2ogr", type = REFERENCE, id = "ogr")
@Resource(file = "/python/zonalstats.py", type = FILE, id = "zonalstats")
@Resource(file = "python", type = REFERENCE, id = "python")

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();

        String file2 = parameter().getString("landuse");
        File file_2 = attachments().getFile(file2);
        String landuse_path = file_2.getPath();

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

        e = resources().getExe("gdal_translate");
        e.addArguments("-ot", "Float32", landuse_path, wd + "/landuse_float.tif");
        e.exec();

        File f_2 = resources().getFile("cfactor");
        e = resources().getExe("python");
        e.addArguments(f_2.getAbsolutePath(), landuse_path, wd + "/landuse_float.tif", wd + "/manning.tif");
        e.exec();

        File f_1 = resources().getFile("zonalstats");
        e = resources().getExe("python");
        e.addArguments(f_1.getAbsolutePath(), wd + "/boundary_5070.shp", wd + "/manning.tif", -9999);
        e.exec();
    }

    // 3) provide the temperature as a result.
    @Override
    protected void postProcess() throws Exception {
        File ws = workspace().getDir();
        results().put(new File(ws, "results.csv"), "curve number");
    }
}