V1_0.java [src/java/m/cfa/trends] Revision: default  Date:
package m.cfa.trends;

import csip.annotations.*;
import static csip.annotations.ResourceType.ARCHIVE;
import static csip.annotations.ResourceType.OUTPUT;
import static csip.annotations.ResourceType.PYTHON3;
import csip.ModelDataService;
import csip.api.server.PayloadParameter;
import csip.api.server.PayloadResults;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.FILE;
import csip.annotations.VersionInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Path;

@Name("trend_statistics")
@Description("CSIP end point for trend assessment of time series data")
@VersionInfo("1.0")
@Path("m/cfa/trends/1.0")
@Resource(type = ARCHIVE, file = "/archive/**")
// @Resource(type = PYTHON3, file = "/archive/trends_WQseries.py",  id = "trends_WQseries")
@Resource(type = PYTHON3, file = "/archive/trends.py",  id = "trends")
public class V1_0 extends ModelDataService {

    File resultFile = null;
    
    @Override
    protected void doProcess() throws Exception {
        String directory = workspace().getDir().toString();
        
        //Get Inputs
        //PayloadParameter inputPayload = parameter();
        //String ts_data = inputPayload.getString("ts_data", "");
        
        //Setup directory/result file
        //File pythonModel = resources().getFile("trends_WQseries");
        File pythonModel = resources().getFile("trends");
        resultFile = new File(directory, "trendsResult.csv");
        
        //Setup inputs to python
        List<String> args = new ArrayList<>();
        args.add("python");
        args.add(pythonModel.getAbsolutePath());
        args.add(directory); 
        args.add(resultFile.getAbsolutePath());

        //Call service
        ProcessBuilder pb = new ProcessBuilder(args);
        Process p = pb.start();
        BufferedReader bfr_error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        int exitCode = p.waitFor();

        //Log any errors (probably not necessary)
        String line;
        String error_log = "";
        while ((line = bfr_error.readLine()) != null) {
            error_log += line + "\n";
            System.err.println(error_log);
        }
        String temp = (error_log.isEmpty()) ? null : error_log;
    }

    @Override
    protected void postProcess() throws Exception {
        PayloadResults resultPayload = results();
        // files (auto added as a download link by csip)
        resultPayload.put(resultFile);
        // values (if you want a simple statistics value, like sens slope, add it this way)
        //resultPayload.put("result_file", resultFile.getName());
    }
}