V2_0.java [src/java/m/watershed/slopelength] 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.slopelength;
import csip.Config;
import csip.api.server.Executable;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.*;
import javax.ws.rs.Path;
import csip.annotations.*;
import java.io.*;
import java.util.logging.Level;
import static m.watershed.Resources.CLEAN;
import static m.watershed.Resources.D8;
import static m.watershed.Resources.GDAL;
import static m.watershed.Resources.LOCATION_MNT_DATA1;
import static m.watershed.Resources.MPI;
import static m.watershed.Resources.OGR;
import static m.watershed.Resources.PITREMOVE;
import static m.watershed.Resources.PYTHON;
/**
* slope length
*
* @author JK (using OD's template)
*/
@Name("slopelength")
@Description("Example of slopelength")
@Path("m/slopelength/2.0")
@Resource(file = "/python/zonalstats.py", type = FILE, id = "zonalstats")
@Resource(file = "/python/slope_length.py", type = FILE, id = "slope_length")
@Resource(file = "/python/cutoff_px.py", type = FILE, id = "cutoff_px")
@Resource(file = "gdalwarp", type = REFERENCE, id = "gdalwarp")
@Resource(file = "gdal_translate", type = REFERENCE, id = "gdal_translate")
@Resource(file = "gdaltransform", type = REFERENCE, id = "gdaltransform")
@Resource(file = "ogr2ogr", type = REFERENCE, id = "ogr")
//@Resource(file = "mpirun", type = REFERENCE, id = "mpirun")
@Resource(file = "python", type = REFERENCE, id = "python")
@Resource(file = "gdal_calc.py", type = REFERENCE, id = "gdal_calc")
@Resource(file = "/bin/lin-amd64/pitremove", type = EXECUTABLE, id = "pitremove")
@Resource(file = "/bin/lin-amd64/d8flowdir", type = EXECUTABLE, id = "d8flowdir")
@Resource(file = "/bin/lin-amd64/gridnet", type = EXECUTABLE, id = "gridnet")
public class V2_0 extends ModelDataService {
File result_file = null;
int act_cores = Runtime.getRuntime().availableProcessors() / 2;
int threads = act_cores + (act_cores / 2);
// 2) watershed
@Override
protected void doProcess() throws Exception {
File ws = workspace().getDir();
String wd = ws.toString();
threads = Config.getInt("mpi.cores", threads);
threads = parameter().getInt("mpi.cores", threads);
String file1 = parameter().getString("boundary");
File file_1 = attachments().getFile(file1);
String boundary_path = file_1.getPath();
try {
Executable ep = resources().getExe(PYTHON);
File e4 = resources().getFile(CLEAN);
LOG.info(" #Cores : " + threads);
LOG.info(" e4_path: " + e4.getPath());
StringWriter err = new StringWriter();
StringWriter output = new StringWriter();
ep.addArguments(e4.getAbsolutePath(),
boundary_path,
workspace().getFile("geometry_validated.shp")
);
ep.redirectError(err);
ep.redirectOutput(output);
ep.redirectDefaults();
if (LOG.isLoggable(Level.INFO)) {
LOG.info(output.toString());
}
if (LOG.isLoggable(Level.SEVERE)) {
LOG.severe(err.toString());
}
int retee = ep.exec();
if (retee != 0) {
throw new ServiceException(" python validation Error : " + err);
}
} catch (ServiceException | IOException et) {
LOG.info(" went wrong");
} finally {
LOG.info(" woohoo worked ;-) ");
}
try {
Executable e = resources().getExe(OGR);
e.addArguments("-t_srs",
"EPSG:5070",
workspace().getFile("boundary.shp"),
workspace().getFile("geometry_validated.shp")
);
e.exec();
} catch (ServiceException | IOException et) {
LOG.info(" went wrong");
} finally {
LOG.info(" woohoo worked ;-) ");
}
try {
StringWriter err = new StringWriter();
StringWriter output = new StringWriter();
Executable e = resources().getExe(GDAL);
e.addArguments(LOCATION_MNT_DATA1 + "/DEM.vrt",
workspace().getFile("DEM_clip0.tif"),
"-crop_to_cutline",
"-cutline",
workspace().getFile("boundary.shp"),
"-multi",
"-of", "GTiff",
"-co", "TILED=YES",
"-co", "COMPRESS=LZW",
"-co", "BIGTIFF=YES",
"-ot", "Float32",
"-co", "NUM_THREADS=" + threads + "",
"-wo", "NUM_THREADS=" + threads + "",
"--config",
"GDAL_CACHEMAX", "1000",
"-wm", "1000"
);
e.redirectError(err);
e.redirectOutput(output);
e.redirectDefaults();
if (LOG.isLoggable(Level.INFO)) {
LOG.info(output.toString());
}
if (LOG.isLoggable(Level.SEVERE)) {
LOG.severe(err.toString());
}
int retee = e.exec();
if (retee != 0) {
throw new ServiceException("Error : " + err);
}
} catch (ServiceException | IOException et) {
LOG.info(" went wrong");
} finally {
LOG.info(" woohoo worked ;-) ");
}
try {
StringWriter err = new StringWriter();
StringWriter output = new StringWriter();
Executable e = resources().getExe(GDAL);
e.addArguments(
"-t_srs", "EPSG:5070",
"-tr", "30", "30",
"-r", "cubicspline",
workspace().getFile("DEM_clip0.tif"),
workspace().getFile("DEM_clip.tif")
);
if (LOG.isLoggable(Level.INFO)) {
LOG.info(output.toString());
}
if (LOG.isLoggable(Level.SEVERE)) {
LOG.severe(err.toString());
e.redirectError(err);
e.redirectOutput(output);
e.redirectDefaults();
}
int retee = e.exec();
if (retee != 0) {
throw new ServiceException("Error : " + err);
}
} catch (ServiceException | IOException et) {
LOG.info(" went wrong");
} finally {
LOG.info(" woohoo worked ;-) ");
}
try {
Executable ee = resources().getExe(MPI);
File e2 = resources().getFile(PITREMOVE);
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", workspace().getFile("DEM_clip.tif"),
"-fel", workspace().getFile("dem_fill.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());
}
} catch (ServiceException | IOException et) {
LOG.info(" went wrong");
} finally {
LOG.info(" woohoo worked ;-) ");
}
try {
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", workspace().getFile("dem_fill.tif"),
"-sd8", workspace().getFile("slope_d8_pre.tif"),
"-p", workspace().getFile("flow_direction_d8_pre.tif")
);
StringWriter err = new StringWriter();
StringWriter output = new StringWriter();
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) {
throw new ServiceException("D8 Error : " + err.toString());
}
} catch (ServiceException | IOException et) {
LOG.info(" went wrong");
} finally {
LOG.info(" woohoo worked ;-) ");
}
// File f_1 = resources().getFile("slope_length");
// e = resources().getExe("python");
// e.addArguments(f_1.getAbsolutePath(), wd + "/p.tif", wd + "/sd8.tif", wd + "/new_slope.tif", wd + "/dir_cut.tif");
// e.exec();
//
// //e = resources().getExe("mpirun");
// //e2 = resources().getFile("gridnet");
// e = resources().getExe("gridnet");
// //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-p", wd+ "/dir_cut.tif", "-plen", wd + "/p_len.tif", "-tlen", wd + "/tlen.tif", "-gord", wd + "/gord.tif");
// e.addArguments("-p", wd + "/dir_cut.tif", "-plen", wd + "/p_len.tif", "-tlen", wd + "/tlen.tif", "-gord", wd + "/gord.tif");
// e.exec();
//
// File f_2 = resources().getFile("cutoff_px");
// e = resources().getExe("python");
// e.addArguments(f_2.getAbsolutePath(), wd + "/p.tif", wd + "/sd8.tif", wd + "/p_len.tif", wd + "/slope_length.tif");
// e.exec();
//
// e = resources().getExe("gdal_translate");
// e.addArguments("-a_nodata", -32768, wd + "/slope_length.tif", wd + "/plen.tif");
// e.exec();
//
// File f_3 = resources().getFile("zonalstats");
// e = resources().getExe("python");
// e.addArguments(f_3.getAbsolutePath(), wd + "/boundary.shp", wd + "/plen.tif", -32768);
// e.exec();
}
// 3) provide the temperature as a result.
@Override
protected void postProcess() throws Exception {
File ws = workspace().getDir();
results().put(new File(ws, "plen.tif"), "slope length raster");
results().put(new File(ws, "dir_cut.tif"), "cutoff direction");
results().put(new File(ws, "results.csv"), "zontal stats results");
}
}