V1_0.java [src/java/m/watershed/hru] 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.hru;

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.*;

/**
 * hru
 *
 * @author JK (using OD's template)
 */
@Name("hru")
@Description("Example of hru")
@Path("m/hru/1.0")
@Resource(file = "/python/zonalstats.py", type = FILE, id = "zonalstats")
@Resource(file = "/python/readmm.py", type = FILE, id = "readmm")
//@Resource(file = "/python/hru.py", type = FILE, id = "hru")
@Resource(file = "gdalwarp", type = REFERENCE, id = "gdalwarp")
@Resource(file = "gdalinfo", type = REFERENCE, id = "gdalinfo")
@Resource(file = "ogr2ogr", type = REFERENCE, id = "ogr")
//@Resource(file = "mpirun", type = REFERENCE, id = "mpirun")
@Resource(file = "python", type = REFERENCE, id = "python")
@Resource(file = "gdal_polygonize.py", type = REFERENCE, id = "gdal_polygonize")
@Resource(file = "gdal_calc.py", type = REFERENCE, id = "gdal_calc")
@Resource(file = "/bin/lin-amd64/gdal_combine", type = EXECUTABLE, id = "gdal_combine")

public class V1_0 extends ModelDataService {

    File result_file = null;

    // 2) watershed
    @Override
    protected void doProcess() throws Exception {

        String ip = request().getRemoteAddr();
        //if ("127.0.0.1".equals(ip)){
        File ws = workspace().getDir();
        String wd = ws.toString();

        String file1 = parameter().getString("boundary_shp");
        File file_1 = attachments().getFile(file1);
        String boundary_shp_path = file_1.getPath();

        String file2 = parameter().getString("boundary_shx");
        File file_2 = attachments().getFile(file2);
        String boundary_shx_path = file_2.getPath();

        String file3 = parameter().getString("boundary_dbf");
        File file_3 = attachments().getFile(file3);
        String boundary_dbf_path = file_3.getPath();

        String file4 = parameter().getString("landuse");
        File file_4 = attachments().getFile(file4);
        String landuse_path = file_4.getPath();

        String file5 = parameter().getString("slope");
        File file_5 = attachments().getFile(file5);
        String slope_path = file_5.getPath();

        String file6 = parameter().getString("soil");
        File file_6 = attachments().getFile(file6);
        String soil_path = file_6.getPath();

        String usr_path = parameter().getString("usrlayer_list");
        String[] ary = usr_path.split("/");

        Executable e = resources().getExe("ogr");
        e.addArguments("-t_srs", "EPSG:5070", "-s_srs", "EPSG:4326", wd + "/boundary_5070.shp", boundary_shp_path);
        e.exec();

        File f_1 = resources().getFile("readmm");
        e = resources().getExe("python");
        e.addArguments(f_1.getAbsolutePath(), wd + "/slope.tif", wd + "/slope_reclass.tif");
        e.exec();

        //e = resources().getExe("mpirun");
        //File e2 = resources().getFile("gdal_combine");
        e = resources().getExe("gdal_combine");
        //e.addArguments("-np", "2", "-host", "localhost", e2.getAbsolutePath(), "-o", "combine.tif", "-csv", "combine_init.csv", landuse_path, wd + "/slope_reclass.tif", soil_path);
        e.addArguments("-o", "combine.tif", "-csv", "combine_init.csv", landuse_path, wd + "/slope_reclass.tif", soil_path);

        for (String ary1 : ary) {

            if (ary1 != "null") {
                String usr_file = parameter().getString(ary1);
                File user_file = attachments().getFile(usr_file);
                String user_path = user_file.getPath();
                e.addArguments(user_path);
            }
        }

        e.exec();

        e = resources().getExe("gdal_calc");
        e.addArguments("-A", wd + "/combine.tif", "--outfile", wd + "/mask.tif", "--overwrite", "--calc", "(1*(A>0))", "--NoDataValue", "0", "--type", "Float32");
        e.exec();

        e = resources().getExe("gdal_polygonize");
        e.addArguments("-8", "-f", "ESRI Shapefile", wd + "/combine.tif", wd + "/hru_5070.shp", "-mask", wd + "/mask.tif");
        e.exec();

        e = resources().getExe("ogr");
        e.addArguments("-t_srs", "EPSG:4326", "-s_srs", "EPSG:5070", wd + "/hru.shp", wd + "/hru_5070.shp");
        e.exec();

        /* File f_2 = resources().getFile("hru");
        e = resources().getExe("python");
        e.addArguments(f_2.getAbsolutePath(), wd + "/hru.shp", wd + "/combine_init.csv");        
        e.exec(); 
        
        e = resources().getExe("mpirun");
        e.addArguments("-np", "2", "-host", "localhost", "/home/ubuntu/taudem/./pitremove", "-z", wd+ "/DEM_clip.tif", "-fel", wd + "/dem_fill.tif");        
        e.exec();
        
        e = resources().getExe("mpirun");
        e.addArguments("-np", "2", "-host", "localhost", "/home/ubuntu/taudem/./d8flowdir", "-fel", wd+ "/dem_fill.tif", "-sd8", wd + "/sd8.tif", "-p", wd + "/p.tif");        
        e.exec();          

        File f_1 = resources().getFile("zonalstats");
        e = resources().getExe("python");
        e.addArguments(f_1.getAbsolutePath(), wd + "/boundary.shp", wd +"/sd8.tif");        
        e.exec(); 
         */
    }

    // 3) provide the temperature as a result.
    @Override
    protected void postProcess() throws Exception {

        File ws = workspace().getDir();
        results().put(new File(ws, "hru.shp"), "hru shp");
        results().put(new File(ws, "hru.shx"), "hru shx");
        results().put(new File(ws, "hru.dbf"), "hru dbf");
        results().put(new File(ws, "combine_init.csv"), "combine_info");

    }

}