Functions.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.utils.Client;
import csip.api.server.Executable;
import csip.api.server.ServiceException;
import csip.SessionLogger;
import csip.utils.JSONUtils;
import java.io.*;
import java.util.Map;
import java.util.logging.Level;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author holm
 */
public class Functions {

    public static boolean Gdal_warp(Integer threads, String sourceFilePath, File outFile, File maskFile, Executable exec, Integer version, String Number, String NoDataVal, SessionLogger LOG) throws Exception {

        try {
            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();

            Executable e = exec;
            if (version == 1) {
                e.addArguments(sourceFilePath,
                        outFile,
                        "-crop_to_cutline",
                        "-cutline",
                        maskFile,
                        "-tr",
                        "30",
                        "30",
                        "-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"
                );
            }
            if (version == 2) {
                e.addArguments(sourceFilePath,
                        outFile,
                        "-crop_to_cutline",
                        "-cutline",
                        maskFile,
                        "-overwrite",
                        "-tr",
                        "30",
                        "30",
                        "-t_srs",
                        "EPSG:5070",
                        "-multi",
                        "-of", "GTiff",
                        "-co", "TILED=YES",
                        "-co", "COMPRESS=LZW",
                        "-co", "BIGTIFF=YES",
                        "-ot", Number,
                        "-co", "NUM_THREADS=" + threads + "",
                        "-wo", "NUM_THREADS=" + threads + "",
                        "--config",
                        "GDAL_CACHEMAX", "1000",
                        "-wm", "1000"
                );
            }
            if (version == 3) {
                e.addArguments(sourceFilePath,
                        outFile,
                        "-crop_to_cutline",
                        "-cutline",
                        maskFile,
                        "-dstnodata",
                        "-3.4028234663852886e+38",
                        "-overwrite",
                        "-tr",
                        "30",
                        "30",
                        "-multi",
                        "-of", "GTiff",
                        "-co", "TILED=YES",
                        "-co", "COMPRESS=LZW",
                        "-co", "BIGTIFF=YES",
                        "-co", "NUM_THREADS=" + threads + "",
                        "-wo", "NUM_THREADS=" + threads + "",
                        "--config",
                        "GDAL_CACHEMAX", "1000",
                        "-wm", "1000"
                );
            }
            if (version == 4) {
                e.addArguments(sourceFilePath,
                        outFile,
                        "-crop_to_cutline",
                        "-cutline",
                        maskFile,
                        "-tr",
                        "10",
                        "10",
                        "-dstnodata",
                        NoDataVal,
                        "-tap",
                        "-s_srs",
                        "EPSG:5070",
                        "-t_srs",
                        "EPSG:5070",
                        "-multi",
                        "-of", "GTiff",
                        "-co", "TILED=YES",
                        "-co", "COMPRESS=LZW",
                        "-co", "BIGTIFF=YES",
                        "-ot", Number,
                        "-co", "NUM_THREADS=" + threads + "",
                        "-wo", "NUM_THREADS=" + threads + "",
                        "-wo", "CUTLINE_ALL_TOUCHED=TRUE",
                        "--config",
                        "GDAL_CACHEMAX", "1000",
                        "-wm", "1000"
                );
            }

            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 ;-) ");
        }
        return true;
    }

    public static boolean Python_zonal(Executable py_exec, File py_script, File maskFile, File sourceFile, String result_ID, String prefix, String category_boolean, SessionLogger LOG) throws Exception {

        Executable ep = py_exec;
        File e5 = py_script;

        StringWriter err = new StringWriter();
        StringWriter output = new StringWriter();

        try {
            ep.addArguments(e5.getAbsolutePath(),
                    maskFile,
                    sourceFile,
                    result_ID,
                    prefix,
                    category_boolean,
                    //"-3.4028234663852886e+38"
                    "-9999"
            );

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(output.toString());
            }
            if (LOG.isLoggable(Level.SEVERE)) {
                LOG.severe(err.toString());
                ep.redirectError(err);
                ep.redirectOutput(output);
                ep.redirectDefaults();
            }

            int retee = ep.exec();

            if (retee != 0) {
                throw new ServiceException(" python zonalstats Error : " + err);
            }
        } catch (ServiceException et) {
            LOG.info(" went wrong");
        } finally {
            LOG.info(" woohoo worked ;-) ");
        }
        return true;
    }

    public static boolean TauDEM_d8DisttoStream(Executable mpi_exec, File tauDEMexec, Integer threads, String outDir, String pFile, String srcFile, String distFile, SessionLogger LOG) throws Exception {
        try {
            Executable ee = mpi_exec;
            File e3 = tauDEMexec;

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(" #Cores : " + threads);
                LOG.info(" e3_path: " + e3.getPath());
            }
            ee.addArguments("--allow-run-as-root",
                    "--path", e3.getParent(),
                    "-wdir", outDir,
                    "--oversubscribe",
                    "-np", threads,
                    e3.getName(),
                    "-p", pFile,
                    "-src", srcFile,
                    "-dist", distFile
            );

            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();

            int retee = ee.exec();

            if (LOG.isLoggable(Level.INFO)) {
                LOG.info(output.toString());
            }
            if (LOG.isLoggable(Level.SEVERE)) {
                LOG.severe(err.toString());
                ee.redirectError(err);
                ee.redirectOutput(output);
                ee.redirectDefaults();
            }
            if (retee != 0) {
                throw new ServiceException("D8STREAMDIST Error : " + err.toString());
            }
        } catch (ServiceException | IOException et) {
            LOG.info(" went wrong");
        } finally {
            LOG.info(" woohoo worked ;-) ");
        }
        return true;
    }

    public static boolean Trans_to_5070(String inFile, String outFile, Executable exec, SessionLogger LOG) throws Exception {
        try {
            StringWriter err = new StringWriter();
            StringWriter output = new StringWriter();

            //ogr2ogr output.shp -t_srs "EPSG:4326" input.shp
            Executable e = exec;
            e.addArguments(outFile,
                    "-t_srs",
                    "EPSG:5070",
                    inFile
            );

            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("Trans_to_5070 Error : " + err);
            }
        } catch (ServiceException et) {
            LOG.info(" went wrong");
        } finally {
            LOG.info(" woohoo worked ;-) ");
        }
        return true;
    }

    public static void ExtractDEM(SessionLogger LOG, File boundary, File result) throws JSONException, Exception {
        JSONArray params = new JSONArray();
        JSONObject data = JSONUtils.data("boundary", boundary.getName());
        params.put(data);
        JSONObject newrequest = JSONUtils.newResponse(params, null, new JSONObject());

        Client cl = new Client();
        JSONObject response = cl.doPOST("http://csip.engr.colostate.edu:8087/csip-dem/d/extract/1.0", newrequest,
                new File[]{
                    boundary,});
        String error = JSONUtils.getErrorStatus(response);
        if (error != null) {
            throw new Exception("Error in csip-dem" + ": " + error);
        }
        Map<String, JSONObject> res = JSONUtils.getResults(response);
        cl.doGET(JSONUtils.getStringParam(res, "DEM.tif", ""), result);
    }
}