V2_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.api.server.ServiceException;
import csip.annotations.Resource;
import javax.ws.rs.Path;
import csip.annotations.*;
import java.io.*;
import java.util.logging.Level;
import static m.watershed.Functions.Gdal_warp;
import static m.watershed.Functions.Python_zonal;
import m.watershed.Resources;
import static m.watershed.Resources.CFACTOR;
import static m.watershed.Resources.CLEAN;
import static m.watershed.Resources.GDAL;
import static m.watershed.Resources.GDALTRANSLATE;
import static m.watershed.Resources.LOCATION_MNT_DATA1;
import static m.watershed.Resources.OGR;
import static m.watershed.Resources.PYTHON;
import static m.watershed.Resources.ZONALSTATS;

/**
 * cfactor
 *
 * @author JK (using OD's template)
 */
@Name("cfactor")
@Description("Example of cfactor")
@Path("m/cfactor/2.0")
@Resource(from = Resources.class)

public class V2_0 extends ModelDataService {

    static final String BOUNDARY = "boundary";

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

        LOG.info("===>  Processing ");

        if (attachments().hasFile(parameter().getString(BOUNDARY))) {

            String result_ID = parameter().getString("result_ID");

            int act_cores = Runtime.getRuntime().availableProcessors() / 2;
            int threads = act_cores + (act_cores / 2);

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

            run(threads, workspace().getDir(), boundary_path, result_ID);
        } else {
            throw new ServiceException("No Boundary input listet!");
        }
    }

    private void run(Integer threads, File outputDir, String boundary_path, String result_ID) throws Exception {

        try {
            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();

            Executable ep = resources().getExe(PYTHON);
            File e4 = resources().getFile(CLEAN);

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(" #Cores : " + threads);
                LOG.info(" e4_path: " + e4.getPath());
            }

            ep.addArguments(e4.getAbsolutePath(),
                    boundary_path,
                    workspace().getFile("geometry_validated.shp")
            );
            ep.redirectError(err);
            ep.redirectOutput(output);
            ep.redirectDefaults();

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(output.toString());
            }
            if (LOG.isLoggable(Level.SEVERE)) {
                LOG.severe(err.toString());
            }

            int retee = ep.exec();

            if (retee != 0) {
                throw new ServiceException(" python validation Error : " + err);
            }
        } catch (ServiceException et) {
            LOG.info(" went wrong");
        } finally {
            LOG.info(" woohoo worked ;-) ");
        }

        try {
            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();

            Executable e = resources().getExe(OGR);
            e.addArguments("-t_srs",
                    "EPSG:5070",
                    workspace().getFile("boundary.shp"),
                    workspace().getFile("geometry_validated.shp")
            );
            e.redirectError(err);
            e.redirectOutput(output);
            e.redirectDefaults();

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(output.toString());
            }
            if (LOG.isLoggable(Level.SEVERE)) {
                LOG.severe(err.toString());
            }

            int retee = e.exec();

            if (retee != 0) {
                throw new ServiceException("OGR 1 Error : " + err);
            }
        } catch (ServiceException et) {
            LOG.info(" went wrong");
        } finally {
            LOG.info(" woohoo worked ;-) ");
        }

        try {

            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();

            Executable e = resources().getExe(OGR);
            e.addArguments("-update",
                    "-append",
                    "-dialect",
                    "sqlite",
                    "-sql",
                    "select extent(geometry) from boundary",
                    workspace().getFile("index.shp"),
                    workspace().getFile("boundary.shp")
            );
            e.redirectError(err);
            e.redirectOutput(output);
            e.redirectDefaults();

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(output.toString());
            }
            if (LOG.isLoggable(Level.SEVERE)) {
                LOG.severe(err.toString());
            }

            int retee = e.exec();

            if (retee != 0) {
                throw new ServiceException("OGR 2 Error : " + err);
            }
        } catch (ServiceException et) {
            LOG.info(" went wrong");
        } finally {
            LOG.info(" woohoo worked ;-) ");
        }

        String sourcePathFile = LOCATION_MNT_DATA1 + "/nlcd_2011.tif";
        boolean st_step = Gdal_warp(threads,
                sourcePathFile,
                workspace().getFile("landuse_clip.tif"),
                workspace().getFile("index.shp"),
                resources().getExe(GDAL),
                1,
                "Float32",
                "0",
                LOG
        );

        try {
            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();

            Executable e = resources().getExe(GDALTRANSLATE);
            e.addArguments("-ot",
                    "Float32",
                    workspace().getFile("landuse_clip.tif"),
                    workspace().getFile("landuse_float.tif"),
                    "-co", "NUM_THREADS=" + threads + "",
                    "--config",
                    "GDAL_CACHEMAX", "1000"
            );
            e.redirectError(err);
            e.redirectOutput(output);
            e.redirectDefaults();

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(output.toString());
            }
            if (LOG.isLoggable(Level.SEVERE)) {
                LOG.severe(err.toString());
            }

            int retee = e.exec();

            if (retee != 0) {
                throw new ServiceException("OGR 1 Error : " + err);
            }
        } catch (ServiceException et) {
            LOG.info(" went wrong");
        } finally {
            LOG.info(" woohoo worked ;-) ");
        }

        try {
            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();

            Executable ep = resources().getExe(PYTHON);
            File e4 = resources().getFile(CFACTOR);

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(" #Cores : " + threads);
                LOG.info(" e4_path: " + e4.getPath());
            }

            ep.addArguments(e4.getAbsolutePath(),
                    workspace().getFile("landuse_clip.tif"),
                    workspace().getFile("landuse_float.tif"),
                    workspace().getFile("manning.tif")
            );
            ep.redirectError(err);
            ep.redirectOutput(output);
            ep.redirectDefaults();

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(output.toString());
            }
            if (LOG.isLoggable(Level.SEVERE)) {
                LOG.severe(err.toString());
            }

            int retee = ep.exec();

            if (retee != 0) {
                throw new ServiceException(" python landuse Error : " + err);
            }
        } catch (ServiceException et) {
            LOG.info(" went wrong");
        } finally {
            LOG.info(" woohoo worked ;-) ");
        }

        boolean Python_zonal = Python_zonal(resources().getExe(PYTHON),
                resources().getFile(ZONALSTATS),
                workspace().getFile("boundary.shp"),
                workspace().getFile("manning.tif"),
                result_ID,
                "cfactor_",
                "False",
                LOG
        );
    }

    @Override
    protected void postProcess() throws Exception {
        File ws = workspace().getDir();
        //results().put(new File(ws, "results.csv"), "curve number");
    }
}