V0_1.java [src/java/m/weps] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package m.weps;

import csip.Config;
import static csip.Config.CSIP_SESSION_TTL;
import csip.api.server.Executable;
import csip.ModelDataService;
import csip.ModelDataServiceConstants;
import csip.api.server.ServiceException;
import csip.annotations.*;
import static csip.annotations.ResourceType.*;
import static csip.annotations.State.DEVELOPMENT;
import csip.utils.Parallel;
import csip.utils.Services;
import csip.utils.ZipFiles;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.ws.rs.*;
import static m.wepsModelConstants.PARMOUTMODELNAME;
import static m.wepsModelConstants.PARMOUTUPDATED;
import org.apache.commons.io.FileUtils;

/**
 * This implements the CSIP WEPS service.
 *
 * @author od, mh
 * This endpoint for testing ONLY
 */
@Name("Weps for ARS")
@Description("WEPS endpoint for Testing ONLY")
@VersionInfo("0.1")
@State(DEVELOPMENT)
@Path("m/weps/0.1")
@Polling(first = 2000, next = 1000)

// ARS versions of the binaries
@Resource(file = "/bin/lin-amd64/ARS/default/2023-01-09/weps", id = "weps-default.lin64", type = EXECUTABLE)
@Resource(file = "/bin/lin-amd64/ARS/dev-version/weps", id = "weps-dev.lin64", type = EXECUTABLE)


public class V0_1 extends ModelDataService implements Executable.StdHandler {

    Executable weps;
    
    // Reminder: add new date to json as well
    static final String SERVICE_UPDATE_DATE = "2/21/2023";
    
    Logger l;
    
    @Override
    public void preProcess() throws Exception {
        
        setProgress("Initializing");
        
        // For Debugging !!!
        if (metainfo().getBoolean("wepsDebugLog", false)) {
            l = Logger.getLogger("csip-core");
            l.setLevel(Level.FINEST);
            try { 
                FileHandler fh;  
                fh = new FileHandler(new File(workspace().getDir(),"wepsdebug-logfil.txt").getAbsolutePath());  
                l.addHandler(fh);
                SimpleFormatter formatter = new SimpleFormatter();  
                fh.setFormatter(formatter);  
            } catch (SecurityException | IOException e) {  
                e.printStackTrace();  
            }  
        }
    }
    
    /**
     *
     * @throws Exception
     */
    @Override
    public void doProcess() throws Exception {
        
        String modelVersion;
        // new versions use metainfo
        if (metainfo().hasName("modelVersion")) {
            modelVersion = metainfo().getString("modelVersion", "");
        } else {
            modelVersion = parameter().getString("modelVersion","");
        }
        
        if (modelVersion.contentEquals("dev-version.lin64")) {
            weps = resources().getExe("weps-dev.lin64");
        } else  if (modelVersion.contentEquals("dev-version")) {
            weps = resources().getExe("weps-dev.lin64");
        } else {
            weps = resources().getExe("weps-default.lin64");
        }

        weps.setStdoutHandler(this);

        String parmHydrologyMethod = "-W" + parameter().getInt("hydrologyMethod", 1);
        String parmResurfacing = "-u" + parameter().getInt("resurfacing", 0);
        String parmInitialization = "-I" + parameter().getInt("initialization", 1);
        String parmConfidenceInterval = "-t" + parameter().getInt("confidenceInterval", 0);
        String parmFurrowEffect = "-T" + parameter().getInt("furrowEffect", 0);
        String parmCalibrationMode = parameter().has("calibrationMode") ? "-C" + parameter().getInt("calibrationMode", 0) : "";
        String parmCalibrationCycles = parameter().has("calibrationCycles") ? "-Z" + parameter().getInt("calibrationCycles", 0) : "";

        weps.setArguments(parmHydrologyMethod,
                parmResurfacing,
                parmInitialization,
                parmConfidenceInterval,
                parmFurrowEffect,
                parmCalibrationMode,
                parmCalibrationCycles,
                "-P" + workspace().getDir()
        );

        int result = weps.exec();
        if (result != 0) {
            throw new ServiceException("WEPS exit error :" + result);
        }
    }


    @Override
    protected void postProcess() throws Exception {
        
        setProgress("Building result data");
        metainfo().put(PARMOUTUPDATED, SERVICE_UPDATE_DATE);
        metainfo().put(PARMOUTMODELNAME, weps.getName());
        metainfo().put("CSIP_SESSION_TTL", Config.getString(CSIP_SESSION_TTL));

        File[] outFiles = workspace().getDir().listFiles( (File file) -> file.getName().endsWith(".out"));
        ArrayList<File> stdFiles2 = new ArrayList(
            Arrays.asList(workspace().getDir().listFiles( (File file) -> 
                file.getName().endsWith("stdout.txt") || 
                file.getName().endsWith("stderr.txt") ||
                file.getName().endsWith("logfil.txt")
            ))
        );
        if (metainfo().getBoolean("wepsDebugLog", false)) {
            // crude, but it works.
            File sysLogFile = new File (Services.getResultsDir(getSUID()), ModelDataServiceConstants.LOG_FILE);
            FileUtils.copyFileToDirectory(sysLogFile, workspace().getDir());
            sysLogFile = new File (workspace().getDir(), ModelDataServiceConstants.LOG_FILE);
            stdFiles2.add(sysLogFile);
        }

        File[] otherRetFiles = workspace().getDir().listFiles( (File file) -> {
            String name = file.getName();
            return (name.endsWith(".cli") || name.endsWith(".win") || name.endsWith(".wdb"));
        });
        File[] subRegionFiles = workspace().getDir().listFiles( (File file) -> file.getName().startsWith("subregion"));
        File erodFilesDir = new File(workspace().getDir(),"sae_in_out_files");
        ArrayList<File> zFilesList = new ArrayList();
        
        boolean subRegionResults = parameter().getBoolean("returnSubRegionResults", false);
        boolean zipResults = parameter().getBoolean("zipResults", false);
        if (zipResults) {
            File zFile = new File(workspace().getDir(),"results.zip");
            
            zFilesList.addAll(Arrays.asList(outFiles));
            zFilesList.addAll(Arrays.asList(otherRetFiles));
            
            ArrayList<File> zFilesListSubReg = new ArrayList();
            ArrayList<File> zFilesListErod = new ArrayList();
            File stdzFile = new File(workspace().getDir(),"logFiles.zip");
            Parallel.run(false,
                () -> {
                    try {
                        if (subRegionResults) {
                            for (File f : subRegionFiles) {
                                File srzFile = new File(workspace().getDir(),f.getName()+".zip");
                                ZipFiles.zip(f,srzFile);
                                zFilesListSubReg.add(srzFile);
                            }
                        }
                    } catch (Exception e) {
                    }
                },
                () -> {
                    try {
                        // These files may be triggered by an option in the .runx file
                        // instead of as a command line option
                        if (erodFilesDir.exists()) {
                            File erzFile = new File(workspace().getDir(),erodFilesDir.getName()+".zip");
                            ZipFiles.zip(erodFilesDir, erzFile);
                            zFilesListErod.add(erzFile);
                        }
                    } catch (Exception e) {
                    }
                },
                () -> {
                    try {
                        if (stdFiles2.size() > 0) {
                            ZipFiles.zip(stdzFile, stdFiles2);
                        }
                    } catch (Exception e) {
                    }
                }
            );

            try {
                zFilesList.addAll(zFilesListSubReg);
                zFilesList.addAll(zFilesListErod);
                zFilesList.add(stdzFile);
                ZipFiles.zip(zFile, zFilesList);
                results().put(zFile);
            } catch (Exception e) {
            }
        } else {
            results().put(outFiles);
            results().put(stdFiles2.toArray(new File[0]));
            results().put(otherRetFiles);
            if (subRegionResults) {
                results().put(subRegionFiles);
            }
            // These files may be triggered by an option in the .runx file
            // instead of as a command line option
            if (erodFilesDir.exists()) {
                results().put(erodFilesDir);
            }
        }
        
        setProgress("Result data ready");

        super.postProcess();
    }

    @Override
    public void handle(String out) {
        String s;
        int start;
        int end;
        
        //Subregion            2 Year          12  of          30
        // or
        //Erosion Year           1  of          30

        if ((start = out.lastIndexOf("Year")) >= 0) {
            start = out.lastIndexOf('\n',start)+1;
            if ((end = out.indexOf('\n', start)) == -1) {
                end = out.length();
            }
            if (out.charAt(end - 1) == '\r') {
                end--;
            }
            s = out.substring(start, end);

            try {
                setProgress(s);
            } catch (ServiceException ex) {
                Logger.getLogger(V0_1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}