V2_0.java [src/java/m/watershed/CO_dist2water] 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.CO_dist2water;
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.*;
import java.io.*;
import java.util.logging.Level;
import static m.watershed.Functions.Gdal_warp;
import static m.watershed.Functions.Python_zonal;
import static m.watershed.Functions.TauDEM_d8DisttoStream;
import m.watershed.Resources;
import static m.watershed.Resources.CLEAN;
import static m.watershed.Resources.D8;
import static m.watershed.Resources.D8STREAMDIST;
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;
import static m.watershed.Resources.ZONALSTATS;
/**
* CO_dist2water
*
* @author JK (using OD's template)
*/
@Name("CO_dist2water")
@Description("Example of CO_dist2water")
@Path("m/CO_dist2water/2.0")
@Resource(from = Resources.class)
public class V2_0 extends ModelDataService {
static final String BOUNDARY = "boundary";
@Override
protected void doProcess() throws Exception {
LOG.info("===> Processing ");
if (attachments().hasFile(parameter().getString(BOUNDARY))) {
int act_cores = Runtime.getRuntime().availableProcessors() / 2;
int threads = act_cores + (act_cores / 2);
File ws = workspace().getDir();
String wd = ws.toString();
String result_ID = parameter().getString("result_ID");
String file1 = parameter().getString("boundary");
File file_1 = attachments().getFile(file1);
String boundary_path = file_1.getPath();
run(threads, workspace().getDir(), boundary_path, result_ID);
} else {
throw new ServiceException("No Boundary input listet!");
}
}
private void run(Integer threads, File outputDir, String boundary_path, String result_ID) throws Exception {
Executable ep = resources().getExe(PYTHON);
File e4 = resources().getFile(CLEAN);
if (LOG.isLoggable(Level.INFO)) {
LOG.info(" #Cores : " + threads);
LOG.info(" e4_path: " + e4.getPath());
}
try {
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 {
Executable e = resources().getExe(OGR);
e.addArguments("-update",
"-append",
"-dialect",
"sqlite",
"-sql",
"select extent(geometry) from boundary",
workspace().getFile("index.shp"),
workspace().getFile("boundary.shp")
);
e.exec();
} catch (ServiceException | IOException et) {
LOG.info(" went wrong");
} finally {
LOG.info(" woohoo worked ;-) ");
}
String sourcePathFile = LOCATION_MNT_DATA1 + "/DEM.vrt";
boolean st_step = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("DEM_clip.tif"),
workspace().getFile("index.shp"),
resources().getExe(GDAL),
1,
"Float32",
"0",
LOG
);
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());
}
try {
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 ;-) ");
}
ee = resources().getExe(MPI);
File e3 = resources().getFile(D8);
if (LOG.isLoggable(Level.INFO)) {
LOG.info(" #Cores : " + threads);
LOG.info(" e3_path: " + e3.getPath());
}
try {
ee.addArguments("--allow-run-as-root",
"--path", e3.getParent(),
"-wdir", workspace().getDir(),
"--oversubscribe",
"-np", threads,
e3.getName(),
"-fel", workspace().getFile("dem_fill.tif"),
"-sd8", workspace().getFile("sd8.tif"),
"-p", workspace().getFile("p.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());
}
} catch (ServiceException | IOException et) {
LOG.info(" went wrong");
} finally {
LOG.info(" woohoo worked ;-) ");
}
sourcePathFile = LOCATION_MNT_DATA1 + "/NHD_Stream_Flow.tif";
boolean Gdal_Stream_step_global = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("water_stream.tif"),
workspace().getFile("index.shp"),
resources().getExe(GDAL),
2,
"INT16",
"0",
LOG
);
boolean d8DisttoStream1 = TauDEM_d8DisttoStream(resources().getExe(MPI),
resources().getFile(D8STREAMDIST),
threads,
workspace().getDir().toString(),
workspace().getFile("p.tif").toString(),
workspace().getFile("water_stream.tif").toString(),
workspace().getFile("dist_stream.tif").toString(),
LOG
);
sourcePathFile = workspace().getFile("dist_stream.tif").toString();
boolean Gdal_Stream_step_local = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("stream_clip.tif"),
workspace().getFile("boundary.shp"),
resources().getExe(GDAL),
3,
"Float32",
"0",
LOG
);
boolean Python_Stream_zonal = Python_zonal(resources().getExe(PYTHON),
resources().getFile(ZONALSTATS),
workspace().getFile("boundary.shp"),
workspace().getFile("stream_clip.tif"),
result_ID,
"dist_to_streams_",
"False",
LOG
);
sourcePathFile = LOCATION_MNT_DATA1 + "/NHD_Swamp_Marsh.tif";
boolean Gdal_Swamp_step_global = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("water_swamp.tif"),
workspace().getFile("index.shp"),
resources().getExe(GDAL),
2,
"INT16",
"0",
LOG
);
boolean d8DisttoStream2 = TauDEM_d8DisttoStream(resources().getExe(MPI),
resources().getFile(D8STREAMDIST),
threads,
workspace().getDir().toString(),
workspace().getFile("p.tif").toString(),
workspace().getFile("water_swamp.tif").toString(),
workspace().getFile("dist_swamp.tif").toString(),
LOG
);
sourcePathFile = workspace().getFile("dist_swamp.tif").toString();
boolean Gdal_Swamp_step_local = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("swamp_clip.tif"),
workspace().getFile("boundary.shp"),
resources().getExe(GDAL),
3,
"Float32",
"0",
LOG
);
boolean Python_Swamp_zonal = Python_zonal(resources().getExe(PYTHON),
resources().getFile(ZONALSTATS),
workspace().getFile("boundary.shp"),
workspace().getFile("swamp_clip.tif"),
result_ID,
"dist_to_swamp_",
"False",
LOG
);
sourcePathFile = LOCATION_MNT_DATA1 + "/NHD_Reservoir.tif";
boolean Gdal_Reservoir_step_global = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("water_reservoir.tif"),
workspace().getFile("index.shp"),
resources().getExe(GDAL),
2,
"INT16",
"0",
LOG
);
boolean d8DisttoStream3 = TauDEM_d8DisttoStream(resources().getExe(MPI),
resources().getFile(D8STREAMDIST),
threads,
workspace().getDir().toString(),
workspace().getFile("p.tif").toString(),
workspace().getFile("water_reservoir.tif").toString(),
workspace().getFile("dist_reservoir.tif").toString(),
LOG
);
sourcePathFile = workspace().getFile("dist_reservoir.tif").toString();
boolean Gdal_Reservoir_step_local = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("reservoir_clip.tif"),
workspace().getFile("boundary.shp"),
resources().getExe(GDAL),
3,
"Float32",
"0",
LOG
);
boolean Python_Reservoir_zonal = Python_zonal(resources().getExe(PYTHON),
resources().getFile(ZONALSTATS),
workspace().getFile("boundary.shp"),
workspace().getFile("reservoir_clip.tif"),
result_ID,
"dist_to_reservoir_",
"False",
LOG
);
sourcePathFile = LOCATION_MNT_DATA1 + "/NHD_Lake_Pond.tif";
boolean Gdal_Lake_step_global = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("water_lake.tif"),
workspace().getFile("index.shp"),
resources().getExe(GDAL),
2,
"INT16",
"0",
LOG
);
boolean d8DisttoStream4 = TauDEM_d8DisttoStream(resources().getExe(MPI),
resources().getFile(D8STREAMDIST),
threads,
workspace().getDir().toString(),
workspace().getFile("p.tif").toString(),
workspace().getFile("water_lake.tif").toString(),
workspace().getFile("dist_lake.tif").toString(),
LOG
);
sourcePathFile = workspace().getFile("dist_lake.tif").toString();
boolean Gdal_Lake_step_local = Gdal_warp(threads,
sourcePathFile,
workspace().getFile("lake_clip.tif"),
workspace().getFile("boundary.shp"),
resources().getExe(GDAL),
3,
"Float32",
"0",
LOG
);
boolean Python_Lake_zonal = Python_zonal(resources().getExe(PYTHON),
resources().getFile(ZONALSTATS),
workspace().getFile("boundary.shp"),
workspace().getFile("lake_clip.tif"),
result_ID,
"dist_to_lake_",
"False",
LOG
);
}
@Override
protected void postProcess() throws Exception {
File ws = workspace().getDir();
//results().put(new File(ws, "results.csv"), "distance to stream");
}
}