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

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.DDM;
import static m.watershed.Resources.DINF;
import static m.watershed.Resources.LOCATION_TAU;
import static m.watershed.Resources.MPI;

/**
 * flow direction
 *
 * @author @author HK (using JK's template (using OD's template))
 */
@Name("dinfflowdir")
@Description("Example of inf flow direction")
@Path("m/dinfflowdir/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);

    @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);
            }

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

            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,
                    "-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);
            }

            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("DINF Error : " + err.toString());
            }

        } else {
            throw new ServiceException("No File listet!");
        }
    }

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

        if (workspace().getFile("slope_inf_pre.tif").exists()) {
            tif_files.add("slope_inf_pre.tif");
        }
        if (workspace().getFile("flow_direction_inf_pre.tif").exists()) {
            tif_files.add("flow_direction_inf_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", ""))
                );

                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", ""));
            }
        }
    }
}