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

import csip.Config;
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.Description;
import csip.annotations.Name;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import m.watershed.Resources;
import static m.watershed.Resources.AREAD8;
import static m.watershed.Resources.AREADINF;
import static m.watershed.Resources.D8;
import static m.watershed.Resources.DDM;
import static m.watershed.Resources.DINF;
import static m.watershed.Resources.GRIDNET;
import static m.watershed.Resources.LOCATION_TAU;
import static m.watershed.Resources.MPI;
import static m.watershed.Resources.PDOUG;

/**
 * flow direction
 *
 * @author @author HK (using JK's template (using OD's template))
 */
@Name("cadel_accum")
@Description("TauDEM DEM flow direction & accumulation")
@Path("m/cadel_accum/1.0")
@Resource(from = Resources.class)
public class V1_0 extends ModelDataService {

    static final String KEY_FILL_DEM = "dem_fill";

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

    boolean peuker = false;

    @Override
    protected void doProcess() throws Exception {

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

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

            threads = Config.getInt("mpi.cores", threads);
            threads = parameter().getInt("mpi.cores", threads);

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

            LOG.info("===>  filled DEM is attached ");

            String file_string = parameter().getString("dem_fill");
            File file_obj = attachments().getFile(file_string);

            String DEM_fill_path = file_obj.getPath();
            String wd = workspace().getDir().toString();

            Executable ee = resources().getExe(MPI);
            File e2 = resources().getFile(D8);

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(" e2_path: " + e2.getPath());
            }

            ee.addArguments("--allow-run-as-root",
                    "--path", e2.getParent(),
                    "-wdir", workspace().getDir(),
                    "--oversubscribe",
                    "-np", threads,
                    e2.getName(),
                    "-fel", DEM_fill_path,
                    "-sd8", workspace().getFile("slope_d8_pre.tif"),
                    "-p", workspace().getFile("flow_direction_d8_pre.tif")
            );

            if (LOCATION_TAU.endsWith("TDCG") || LOCATION_TAU.endsWith("TDMX")) {
                ee.addArguments("-ddm", DDM);
            }

            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();
            ee.redirectError(err);
            ee.redirectOutput(output);
            ee.redirectDefaults();

            int retee = ee.exec();

            System.out.println(" " + output.toString());
            System.out.println(" " + err.toString());

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

            if (retee != 0) {
                throw new ServiceException("D8 Error : " + err.toString());
            }

            //---------------------------------------------------
            ee = null;
            ee = resources().getExe(MPI);
            File e3 = resources().getFile(AREAD8);

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(" e3_path: " + e3.getPath());
            }

            ee.addArguments("--allow-run-as-root",
                    "--path", e3.getParent(),
                    "-wdir", workspace().getDir(),
                    "--oversubscribe",
                    "-np", threads,
                    e3.getName(),
                    "-p", workspace().getFile("flow_direction_d8_pre.tif"),
                    "-ad8", workspace().getFile("flow_accumulation_d8_pre.tif"),
                    "-nc"
            );

            err = new StringWriter();
            output = new StringWriter();
            ee.redirectError(err);
            ee.redirectOutput(output);
            ee.redirectDefaults();

            retee = ee.exec();

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

            if (retee != 0) {
                throw new ServiceException("areaD8 Error : " + err.toString());
            }

            //----------------------------------------------------------
            ee = null;
            ee = resources().getExe(MPI);
            File e22 = resources().getFile(DINF);

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(" e22_path: " + e22.getPath());
            }

            ee.addArguments("--allow-run-as-root",
                    "--path", e22.getParent(),
                    "-wdir", workspace().getDir(),
                    "--oversubscribe",
                    "-np", threads,
                    e22.getName(),
                    "-fel", DEM_fill_path,
                    "-slp", workspace().getFile("slope_inf_pre.tif"),
                    "-ang", workspace().getFile("flow_direction_inf_pre.tif")
            );
            if (LOCATION_TAU.endsWith("TDCG") || LOCATION_TAU.endsWith("TDMX")) {
                ee.addArguments("-ddm", DDM);
            }

            err = new StringWriter();
            output = new StringWriter();
            ee.redirectError(err);
            ee.redirectOutput(output);
            ee.redirectDefaults();

            retee = ee.exec();

            System.out.println(" " + output.toString());
            System.out.println(" " + err.toString());

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

            if (retee != 0) {
                throw new ServiceException("DINF Error : " + err.toString());
            }

            //---------------------------------------------------
            ee = null;
            ee = resources().getExe(MPI);
            File e33 = resources().getFile(AREADINF);

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(" e33_path: " + e33.getPath());
            }

            ee.addArguments("--allow-run-as-root",
                    "--path", e33.getParent(),
                    "-wdir", workspace().getDir(),
                    "--oversubscribe",
                    "-np", threads,
                    e33.getName(),
                    "-ang", workspace().getFile("flow_direction_inf_pre.tif"),
                    "-sca", workspace().getFile("flow_accumulation_inf_pre.tif"),
                    "-nc"
            );

            err = new StringWriter();
            output = new StringWriter();
            ee.redirectError(err);
            ee.redirectOutput(output);
            ee.redirectDefaults();

            retee = ee.exec();

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

            if (retee != 0) {
                throw new ServiceException("areaDINF Error : " + err.toString());
            }

            //------------------------------------------------------------
            ee = null;
            ee = resources().getExe(MPI);
            File e4 = resources().getFile(GRIDNET);

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

            ee.addArguments("--allow-run-as-root",
                    "--path", e4.getParent(),
                    "-wdir", workspace().getDir(),
                    "--oversubscribe",
                    "-np", threads,
                    e4.getName(),
                    "-p", workspace().getFile("flow_direction_d8_pre.tif"),
                    //"-plen", workspace().getFile("longest_flow_kength_up_pre.tif"),
                    //"-tlen", workspace().getFile("total_path_length_up_pre.tif"),
                    "-gord", workspace().getFile("strahler_order_pre.tif")
            );

            err = new StringWriter();
            output = new StringWriter();
            ee.redirectError(err);
            ee.redirectOutput(output);
            ee.redirectDefaults();

            retee = ee.exec();

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

            if (retee != 0) {
                throw new ServiceException("gridnet Error : " + err.toString());
            }
            //-------------------------------------------------------

            if (peuker) {
                ee = null;
                ee = resources().getExe(MPI);
                File e5 = resources().getFile(PDOUG);

                if (LOG.isLoggable(Level.INFO)) {
                    LOG.info(" e5_path: " + e5.getPath());
                }

                ee.addArguments("--allow-run-as-root",
                        "--path", e5.getParent(),
                        "-wdir", workspace().getDir(),
                        "--oversubscribe",
                        "-np", threads,
                        e5.getName(),
                        "-fel", DEM_fill_path,
                        "-ad8", workspace().getFile("flow_accumulation_d8_pre.tif"),
                        "-ss", workspace().getFile("peuker_douglas_pre.tif")
                );
                if (LOCATION_TAU.endsWith("TDCG") || LOCATION_TAU.endsWith("TDMX")) {
                    ee.addArguments("-ddm", DDM);
                }

                err = new StringWriter();
                output = new StringWriter();
                ee.redirectError(err);
                ee.redirectOutput(output);
                ee.redirectDefaults();

                retee = ee.exec();

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

                if (retee != 0) {
                    throw new ServiceException("peukerdouglas Error : " + err.toString());
                }
            }
        } else {
            throw new ServiceException("No Filled DEM listet!");
        }
    }

    @Override
    protected void postProcess() throws Exception {
        List<String> tif_files = new ArrayList<>();

        if (workspace().getFile("slope_d8_pre.tif").exists()) {
            tif_files.add("slope_d8_pre.tif");
            //results().put(workspace().getFile("slope_d8_pre.tif"), "slope raster");
        }
        if (workspace().getFile("slope_inf_pre.tif").exists()) {
            tif_files.add("slope_inf_pre.tif");
            //results().put(workspace().getFile("slope_d8_pre.tif"), "slope raster");
        }
        if (workspace().getFile("flow_direction_d8_pre.tif").exists()) {
            tif_files.add("flow_direction_d8_pre.tif");
            //results().put(workspace().getFile("flow_direction_d8_pre.tif"), "flow direction raster");
        }
        if (workspace().getFile("flow_direction_inf_pre.tif").exists()) {
            tif_files.add("flow_direction_inf_pre.tif");
            //results().put(workspace().getFile("flow_direction_d8_pre.tif"), "flow direction raster");
        }
        if (workspace().getFile("flow_accumulation_d8_pre.tif").exists()) {
            tif_files.add("flow_accumulation_d8_pre.tif");
            //results().put(workspace().getFile("flow_accumulation_d8_pre.tif"), "flow accumulation raster");
        }
        if (workspace().getFile("flow_accumulation_inf_pre.tif").exists()) {
            tif_files.add("flow_accumulation_inf_pre.tif");
            //results().put(workspace().getFile("flow_accumulation_d8_pre.tif"), "flow accumulation raster");
        }
        if (workspace().getFile("longest_flow_kength_up_pre.tif").exists()) {
            tif_files.add("longest_flow_kength_up_pre.tif");
            //results().put(workspace().getFile("longest_flow_kength_up_pre.tif"), "longest_flow_kength_up raster");
        }
        if (workspace().getFile("total_path_length_up_pre.tif").exists()) {
            tif_files.add("total_path_length_up_pre.tif");
            //results().put(workspace().getFile("total_path_length_up_pre.tif"), "total_path_length_up raster");
        }
        if (workspace().getFile("strahler_order_pre.tif").exists()) {
            tif_files.add("strahler_order_pre.tif");
            //results().put(workspace().getFile("strahler_order_pre.tif"), "strahler_order raster");
        }
        if (workspace().getFile("peuker_douglas_pre.tif").exists()) {
            tif_files.add("peuker_douglas_pre.tif");
            //results().put(workspace().getFile("peuker_douglas_pre.tif"), "peukerdouglas raster");
        }

        for (String tif_file : tif_files) {
            String type = "";

            try {
                StringWriter err = new StringWriter();
                StringWriter output = new StringWriter();
                Executable eg2 = resources().getExe("gdalwarp");
                if (!tif_file.contains("direction") || !tif_file.contains("strahler")) {
                    eg2.addArguments("-overwrite",
                            type,
                            "-multi",
                            "-ot", "Float32",
                            "-co", "NUM_THREADS=" + threads + "",
                            "-wo", "NUM_THREADS=" + threads + "",
                            "--config",
                            "GDAL_CACHEMAX", "3000",
                            "-wm", "3000",
                            "-of", "GTiff",
                            "-co", "TILED=YES",
                            "-co", "COMPRESS=LZW",
                            "-co", "BIGTIFF=YES",
                            "-dstnodata", "-9999",
                            workspace().getFile(tif_file),
                            workspace().getFile(tif_file.replace("_pre", ""))
                    );
                } else {
                    eg2.addArguments("-overwrite",
                            type,
                            "-multi",
                            "-ot", "Int16",
                            "-co", "NUM_THREADS=" + threads + "",
                            "-wo", "NUM_THREADS=" + threads + "",
                            "--config",
                            "GDAL_CACHEMAX", "3000",
                            "-wm", "3000",
                            "-of", "GTiff",
                            "-co", "TILED=YES",
                            "-co", "COMPRESS=LZW",
                            "-co", "BIGTIFF=YES",
                            "-dstnodata", "-9999",
                            workspace().getFile(tif_file),
                            workspace().getFile(tif_file.replace("_pre", ""))
                    );
                }
                eg2.redirectError(err);
                eg2.redirectOutput(output);
                eg2.redirectDefaults();

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

                int retee = eg2.exec();

                if (retee != 0) {
                    throw new ServiceException("Error : " + err);
                }
            } catch (ServiceException | IOException e) {
                LOG.info(" went wrong" + tif_file);
            } finally {
                LOG.info(" " + tif_file);
                results().put(workspace().getFile(tif_file.replace("_pre", "")), tif_file.replace("_pre", ""));
            }
        }
    }
}