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

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

/**
 * wildfire
 *
 * @author JK
 */
@Name("wildfire")
@Description("wildfire")
@Path("m/wildfire/1.0")
@Resource(file = "/python/zonalstats_cat.py", type = FILE, id = "zonalstats")
@Resource(file = "gdalwarp", type = REFERENCE, id = "gdalwarp")
@Resource(file = "python", type = REFERENCE, id = "python")

public class V1_0 extends ModelDataService {

    File result_file = null;

    @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 whp_type = parameter().getString("whp_type");
        String whp_path = "/mnt/csip-watershed/whp2.tif";
        int no_data = -9999;

        if ("continuous".equals(whp_type)) {
            whp_path = "/mnt/csip-watershed/whp2014_cnt";
            no_data = -2147483647;
        }

        File f_1 = resources().getFile("zonalstats");
        Executable e = resources().getExe("python");
        LOG.info(f_1.getAbsolutePath());
        LOG.info(boundary_path);
        LOG.info(whp_path);
        LOG.info(Integer.toString(no_data));

        e.addArguments(f_1.getAbsolutePath(), boundary_path, whp_path, "results_1.csv", "False", no_data);
        e.exec();

        e = resources().getExe("python");
        e.addArguments(f_1.getAbsolutePath(), boundary_path, whp_path, "results_cat.csv", "True", no_data);
        e.exec();

        e = resources().getExe("gdalwarp");
        e.addArguments(whp_path, wd + "/watershed_whp.tif", "-crop_to_cutline", "-cutline", boundary_path, "-multi");
        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_1.csv"), "wildfire_1");
        results().put(new File(ws, "results_cat.csv"), "wildfire_2");
        results().put(new File(ws, "watershed_whp.tif"), "wildfire_3");
    }

}