V2_0.java [src/java/m/watershed/d8flowdir] 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.d8flowdir;
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.D8;
import static m.watershed.Resources.GDAL;
import static m.watershed.Resources.MPI;
/**
* flow direction
*
* @author @author HK (using JK's template (using OD's template))
*/
@Name("d8flowdir")
@Description("Example of eight flow direction")
@Path("m/d8flowdir/2.0")
@Resource(from = Resources.class)
public class V2_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);
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(" #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(),
"-fel", DEM_fill_path,
"-sd8", workspace().getFile("slope_d8_pre.tif"),
"-p", workspace().getFile("flow_direction_d8_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("D8 Error : " + err.toString());
}
err = new StringWriter();
output = new StringWriter();
Executable eg1 = resources().getExe(GDAL);
eg1.addArguments("-overwrite",
"-multi",
"-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("slope_d8_pre.tif"),
workspace().getFile("slope_d8.tif")
);
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 D8 GDAL slope : " + err);
}
err = new StringWriter();
output = new StringWriter();
Executable eg2 = resources().getExe("gdalwarp");
eg2.addArguments("-overwrite",
"-ot", "Int16",
"-multi",
"-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("flow_direction_d8_pre.tif"),
workspace().getFile("flow_direction_d8.tif")
);
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());
}
retee = eg2.exec();
if (retee != 0) {
throw new ServiceException("Error D8 GDAL flow direction : " + err);
}
} else {
throw new ServiceException("No Filled DEM listet!");
}
}
@Override
protected void postProcess() throws Exception {
File ws = workspace().getDir();
results().put(workspace().getFile("slope_d8.tif"), "slope raster");
results().put(workspace().getFile("flow_direction_d8.tif"), "flow direction raster");
}
}