Utils.java [src/java/m/watershed] 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;
import csip.api.server.Executable;
import csip.api.server.ServiceException;
import csip.api.server.ServiceResources;
import csip.SessionLogger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import org.apache.commons.io.FileUtils;
/**
*
* @author od
*/
public class Utils {
/**
* Execution wrapper.
*
* @param r
* @param LOG
* @param exe
* @param args
* @throws ServiceException
* @throws IOException
*/
public static void exec(ServiceResources r, SessionLogger LOG, String exe, Object... args)
throws ServiceException, IOException {
Executable e = r.getExe(exe);
e.addArguments(args);
int ret = e.exec();
if (LOG.isLoggable(Level.INFO)) {
if (e.stdout().exists()) {
LOG.info(FileUtils.readFileToString(e.stdout(), "UTF-8"));
}
}
if (ret != 0 && !exe.equals("mpirun")) {
String err = "";
if (e.stderr().exists()) {
err = FileUtils.readFileToString(e.stderr(), "UTF-8");
}
throw new ServiceException(exe + ": " + ret + " " + err);
}
}
/**
* Check if files exist in dir.
*
* @param dir
* @param files
* @throws ServiceException
*/
public static void checkExist(File dir, String... files) throws ServiceException {
for (String file : files) {
if (!new File(dir, file).exists()) {
throw new ServiceException("File not found: " + file);
}
}
}
/**
* Check if files exist in dir.
*
* @param dir
* @param files
* @throws ServiceException
*/
public static void checkExistPoint(File dir, String... files) throws ServiceException {
for (String file : files) {
if (!new File(dir, file).exists()) {
throw new ServiceException("OutletPoint needs to be adjusted, or use accurate location feature.");
}
}
}
/**
* Check if files exist in dir.
*
* @param dir
* @return
* @throws ServiceException
* @throws java.io.FileNotFoundException
*/
public static String rasterExtent(File dir) throws ServiceException, FileNotFoundException, IOException {
String minX = "";
String minY = "";
String maxX = "";
String maxY = "";
File[] listFiles = dir.listFiles();
String needed_gdalinfo_file = "";
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].isFile()) {
String fileName = listFiles[i].getName();
if (fileName.startsWith("gdalinfo-") && fileName.endsWith("stdout.txt")) {
needed_gdalinfo_file = fileName;
}
}
}
try ( BufferedReader b = new BufferedReader(new FileReader(new File(dir, needed_gdalinfo_file)))) {
String readLine = "";
while ((readLine = b.readLine()) != null) {
if (readLine.contains("Lower Left")) {
String[] ul_parts = readLine.split("\\(");
String[] coords = (ul_parts[1]).replaceAll("\\)", " ").trim().split(",");
minX = coords[0];
minY = coords[1];
}
if (readLine.contains("Upper Right")) {
String[] ul_parts = readLine.split("\\(");
String[] coords = (ul_parts[1]).replaceAll("\\)", " ").trim().split(",");
maxX = coords[0];
maxY = coords[1];
}
}
}
if (new File(dir, needed_gdalinfo_file).delete()) {
System.out.println("File deleted successfully");
} else {
throw new ServiceException("File not found: " + new File(dir, needed_gdalinfo_file));
}
return minX + " " + minY + " " + maxX + " " + maxY;
}
}