V1_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 com.opencsv.CSVWriter;
import csip.api.server.Executable;
import csip.ModelDataService;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.*;
import javax.ws.rs.Path;
import csip.annotations.*;
import java.io.*;
import java.nio.channels.FileChannel;
/**
* CO_dist2water
*
* @author JK (using OD's template)
*/
@Name("CO_dist2water")
@Description("Example of CO_dist2water")
@Path("m/CO_dist2water/1.0")
@Resource(file = "/python/zonalstats.py", type = FILE, id = "zonalstats")
@Resource(file = "/python/slope_length.py", type = FILE, id = "slope_length")
@Resource(file = "/python/LS.py", type = FILE, id = "LS_factor")
@Resource(file = "gdalwarp", type = REFERENCE, id = "gdalwarp")
@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")
@Resource(file = "/bin/lin-amd64/d8hdisttostrm", type = EXECUTABLE, id = "d8hdisttostrm")
public class V1_0 extends ModelDataService {
File result_file = null;
// 2) watershed
@Override
protected void doProcess() throws Exception {
File ws = workspace().getDir();
String wd = ws.toString();
String bound_4326 = parameter().getString("dem_bound_wkt");
String file1 = parameter().getString("field_boundary");
File file_1 = attachments().getFile(file1);
String boundary_path = file_1.getPath();
File sourceFile = new File("/mnt/csip-watershed/bound.vrt");
File destFile = new File(wd + "/bound.vrt");
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
String outputFile = wd + "/bound.csv";
try (CSVWriter csvOutput = new CSVWriter(new FileWriter(outputFile), ';')) {
String[] entries = "Name#Polygon".split("#");
csvOutput.writeNext(entries);
String wkt_row = "Polygon#" + bound_4326;
String[] entries_2 = wkt_row.split("#");
csvOutput.writeNext(entries_2);
csvOutput.close();
}
Executable e = resources().getExe("gdalwarp");
e.addArguments("/mnt/csip-watershed/DEM.vrt", wd + "/DEM_clip.tif", "-crop_to_cutline", "-cutline", wd + "/bound.vrt", "-tr", 30, 30, "-multi");
e.exec();
e = resources().getExe("pitremove");
e.addArguments("-z", wd + "/DEM_clip.tif", "-fel", wd + "/dem_fill.tif");
e.exec();
e = resources().getExe("d8flowdir");
e.addArguments("-fel", wd + "/dem_fill.tif", "-sd8", wd + "/sd8.tif", "-p", wd + "/p.tif");
e.exec();
e = resources().getExe("gdalwarp");
e.addArguments("/mnt/csip-watershed/NHD_Stream_Flow.tif", wd + "/water_stream.tif", "-ot", "INT16", "-overwrite", "-tr", 30, 30, "-crop_to_cutline", "-cutline", wd + "/bound.vrt", "-multi");
e.exec();
e = resources().getExe("d8hdisttostrm");
e.addArguments("-p", wd + "/p.tif", "-src", wd + "/water_stream.tif", "-dist", wd + "/dist_stream.tif");
e.exec();
e = resources().getExe("gdalwarp");
e.addArguments(wd + "/dist_stream.tif", wd + "/stream_clip.tif", "-overwrite", "-crop_to_cutline", "-cutline", boundary_path, "-dstnodata", "-3.4028234663852886e+38");
e.exec();
File f = resources().getFile("zonalstats");
e = resources().getExe("python");
e.addArguments(f.getAbsolutePath(), boundary_path, wd + "/stream_clip.tif", -3.4028234663852886e+38);
e.exec();
e = resources().getExe("gdalwarp");
e.addArguments("/mnt/csip-watershed/NHD_Swamp_Marsh.tif", wd + "/water_swamp.tif", "-ot", "INT16", "-overwrite", "-tr", 30, 30, "-crop_to_cutline", "-cutline", wd + "/bound.vrt", "-multi");
e.exec();
e = resources().getExe("d8hdisttostrm");
e.addArguments("-p", wd + "/p.tif", "-src", wd + "/water_swamp.tif", "-dist", wd + "/dist_swamp.tif");
e.exec();
e = resources().getExe("gdalwarp");
e.addArguments(wd + "/dist_swamp.tif", wd + "/swamp_clip.tif", "-overwrite", "-crop_to_cutline", "-cutline", boundary_path, "-dstnodata", "-3.4028234663852886e+38");
e.exec();
f = resources().getFile("zonalstats");
e = resources().getExe("python");
e.addArguments(f.getAbsolutePath(), boundary_path, wd + "/swamp_clip.tif", -3.4028234663852886e+38);
e.exec();
e = resources().getExe("gdalwarp");
e.addArguments("/mnt/csip-watershed/NHD_Reservoir.tif", wd + "/water_reservoir.tif", "-ot", "INT16", "-overwrite", "-tr", 30, 30, "-crop_to_cutline", "-cutline", wd + "/bound.vrt", "-multi");
e.exec();
e = resources().getExe("d8hdisttostrm");
e.addArguments("-p", wd + "/p.tif", "-src", wd + "/water_reservoir.tif", "-dist", wd + "/dist_reservoir.tif");
e.exec();
e = resources().getExe("gdalwarp");
e.addArguments(wd + "/dist_reservoir.tif", wd + "/reservoir_clip.tif", "-overwrite", "-crop_to_cutline", "-cutline", boundary_path, "-dstnodata", "-3.4028234663852886e+38");
e.exec();
f = resources().getFile("zonalstats");
e = resources().getExe("python");
e.addArguments(f.getAbsolutePath(), boundary_path, wd + "/reservoir_clip.tif", -3.4028234663852886e+38);
e.exec();
e = resources().getExe("gdalwarp");
e.addArguments("/mnt/csip-watershed/NHD_Lake_Pond.tif", wd + "/water_lake.tif", "-ot", "INT16", "-overwrite", "-tr", 30, 30, "-crop_to_cutline", "-cutline", wd + "/bound.vrt", "-multi");
e.exec();
e = resources().getExe("d8hdisttostrm");
e.addArguments("-p", wd + "/p.tif", "-src", wd + "/water_lake.tif", "-dist", wd + "/dist_lake.tif");
e.exec();
e = resources().getExe("gdalwarp");
e.addArguments(wd + "/dist_lake.tif", wd + "/lake_clip.tif", "-overwrite", "-crop_to_cutline", "-cutline", boundary_path, "-dstnodata", "-3.4028234663852886e+38");
e.exec();
f = resources().getFile("zonalstats");
e = resources().getExe("python");
e.addArguments(f.getAbsolutePath(), boundary_path, wd + "/lake_clip.tif", -3.4028234663852886e+38);
e.exec();
}
// 3) provide the temperature as a result.
@Override
protected void postProcess() throws Exception {
File ws = workspace().getDir();
results().put(new File(ws, "results.csv"), "distance to stream");
}
}