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

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

/**
 * Flow accumulate
 *
 * @author @author HK (using JK's template (using OD's template))
 */
@Name("flow_accumulate")
@Description("Example of eight direction flow accumulation")
@Path("m/flow_accumulate/2.0")
@Resource(from = Resources.class)

public class V2_0 extends ModelDataService {

    static final String KEY_FLOW = "flow_8_dir";
    static final String KEY_OUT = "outlet_point";
    static final String KEY_PEUKER = "peuker_douglas";
    static final String KEY_NC = "nc";

    boolean outlet_data = false;
    boolean weighted_data = false;
    String st_nc = "";

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

    @Override
    protected void doProcess() throws Exception {

        String file_string = "-";

        if (parameter().has(KEY_OUT)) {
            outlet_data = true;
        }

        if (parameter().has(KEY_PEUKER)) {
            weighted_data = true;
        }

        if (parameter().has(KEY_NC)) {
            st_nc = "-nc";
        }

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

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

            file_string = parameter().getString(KEY_FLOW);
            File file_obj = attachments().getFile(file_string);
            String flow_8dir_path = file_obj.getPath();

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

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

            if (outlet_data) {
                System.out.println(" OUTLET in request");
                String outfile_string = parameter().getString(KEY_OUT);
                File outfile_obj = attachments().getFile(outfile_string);
                String outlet_path = outfile_obj.getPath();

                if (outlet_data && !weighted_data) {
                    ee.addArguments("--allow-run-as-root",
                            "--path", e2.getParent(),
                            "-wdir", workspace().getDir(),
                            "--oversubscribe",
                            "-np", threads,
                            e2.getName(),
                            "-p", flow_8dir_path,
                            "-ad8", workspace().getFile("wsh_flow_accumulation_ad8_pre.tif"),
                            "-o", outlet_path,
                            "-nc"
                    );
                }

                if (outlet_data && weighted_data) {
                    System.out.println(" OUTLET in request && Weighted Data");
                    String peukfile_string = parameter().getString(KEY_PEUKER);
                    File peukfile_obj = attachments().getFile(peukfile_string);
                    String peuker_path = peukfile_obj.getPath();
                    ee.addArguments("--allow-run-as-root",
                            "--path", e2.getParent(),
                            "-wdir", workspace().getDir(),
                            "--oversubscribe",
                            "-np", threads,
                            e2.getName(),
                            "-p", flow_8dir_path,
                            "-ad8", workspace().getFile("weighted_area_ad8_pre.tif"),
                            "-o", outlet_path,
                            "-wg", peuker_path,
                            "-nc"
                    );
                }
            } else {
                System.out.println(" NO outlet in request ");
                ee.addArguments("--allow-run-as-root",
                        "--path", e2.getParent(),
                        "-wdir", workspace().getDir(),
                        "--oversubscribe",
                        "-np", threads,
                        e2.getName(),
                        "-p", flow_8dir_path,
                        "-ad8", workspace().getFile("flow_accumulation_d8_pre.tif"),
                        "-nc"
                );
            }

            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());
                ee.redirectError(err);
                ee.redirectOutput(output);
                ee.redirectDefaults();
            }

            if (retee != 0) {
                ee.redirectError(err);
                ee.redirectOutput(output);
                ee.redirectDefaults();
                throw new ServiceException("Error AreaD8: " + err.toString());
            }
        } else {
            throw new ServiceException("No File listet!");
        }
    }

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

        if (workspace().getFile("wsh_flow_accumulation_ad8_pre.tif").exists()) {
            tif_files.add("wsh_flow_accumulation_ad8_pre.tif");
        }
        if (workspace().getFile("weighted_area_ad8_pre.tif").exists()) {
            tif_files.add("weighted_area_ad8_pre.tif");
        }
        if (workspace().getFile("flow_accumulation_d8_pre.tif").exists()) {
            tif_files.add("flow_accumulation_d8_pre.tif");
        }
        for (String tif_file : tif_files) {
            String type = "";

            try {
                StringWriter err = new StringWriter();
                StringWriter output = new StringWriter();
                Executable eg2 = resources().getExe("gdalwarp");
                eg2.addArguments("-overwrite",
                        type,
                        "-multi",
                        "-ot", "Float32",
                        "-co", "NUM_THREADS=" + threads + "",
                        "-wo", "NUM_THREADS=" + threads + "",
                        "--config",
                        "GDAL_CACHEMAX", "2000",
                        "-wm", "2000",
                        "-of", "GTiff",
                        "-co", "TILED=YES",
                        "-co", "COMPRESS=LZW",
                        "-co", "BIGTIFF=YES",
                        "-dstnodata", "-9999",
                        workspace().getFile(tif_file),
                        workspace().getFile(tif_file.replace("_pre", ""))
                );

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

                int retee = eg2.exec();

                if (retee != 0) {
                    throw new ServiceException("Error gdalwarp: " + 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", ""));
            }
        }
    }
}