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

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.logging.Level;
import m.watershed.Resources;
import static m.watershed.Resources.*;

/**
 * Fill sinks
 *
 * @author HK (using JK's template (using OD's template))
 */
@Name("fill_sinks")
@Description("Example of an fill sinks")
@Path("m/fill_sinks/2.0")
@Resource(from = Resources.class)
public class V2_0 extends ModelDataService {

    static final String KEY_DEM = "dem";
    static final String KEY_OUTNAME = "result_file_name";

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

    @Override
    protected void doProcess() throws Exception {

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

        if (attachments().hasFile(parameter().getString(KEY_DEM))) {
            threads = Config.getInt("mpi.cores", threads);
            threads = parameter().getInt("mpi.cores", threads);

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

            result_file_name = parameter().getString(KEY_OUTNAME, "dem_fill.tif");
            LOG.info("===>  Output file name: " + result_file_name);

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

            String orig_DEM_path = file_obj.getPath();

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

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

            ee.addArguments("--allow-run-as-root",                    
                    "--path", e2.getParent(),
                    "-wdir", workspace().getDir(),
                    "--oversubscribe",
                    "-np", threads,
                    e2.getName(),
                    "-z", orig_DEM_path,
                    "-fel", workspace().getFile("dem_filled_pre.tif")
            );

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

            int retee = ee.exec();

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

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

            err = new StringWriter();
            output = new StringWriter();
            
            Executable eg1 = resources().getExe(GDAL);
            eg1.addArguments("-overwrite",
                    "-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.0",
                    workspace().getFile("dem_filled_pre.tif"),
                    workspace().getFile(result_file_name));

            eg1.redirectError(err);
            eg1.redirectOutput(output);
            eg1.redirectDefaults();
            
            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(output.toString());
            }
            if (LOG.isLoggable(Level.SEVERE)) {
                LOG.severe(err.toString());
            }
            
            retee = eg1.exec();
            if (retee != 0) {
                throw new ServiceException("Error : " + err.toString());
            }
        } else {
            throw new ServiceException("No DEM File listet!");
        }
    }

    @Override
    protected void postProcess() throws Exception {
        results().put(workspace().getFile(result_file_name), "Filled DEM");
    }
}