Displaying differences for changeset
 
display as  

src/java/m/weps/V1_3.java

@@ -120,6 +120,7 @@
             // refactor & make smaller
             @Override
             public String call() throws Exception {
+                String error = null;
                 ProcessComponent pc = new ProcessComponent();
                 try {
                     db = PostGIS.singleton();
@@ -143,67 +144,71 @@
                     loadRequiredParameters();
 
                     // get the mgmt as a json obj and create a WEPS man file using Jim's parser
-                    loadWepsMgmt();
+                    error = loadWepsMgmt();
+                    LOG.info("ERROR: "+ error);
+                    if(error == null){
+                        // Get field geometry
+                        getFieldGeometry();
 
-                    // Get field geometry
-                    getFieldGeometry();
+                        // get wind barriers
+                        getWindBarriers();
 
-                    // get wind barriers
-                    getWindBarriers();
+                        // Get climate info
+                        getClimate();
 
-                    // Get climate info
-                    getClimate();
+                        // Get wind info
+                        generateWindData();
 
-                    // Get wind info
-                    generateWindData();
+                        // Get soils data
+                        getSoilIfc();
 
-                    // Get soils data
-                    getSoilIfc();
+                        // run WEPS model
+                        try {
+                            // Always generate Weps Run file from JSON parameters (it is never provided in the model srvc signature)
+                            LOG.log(Level.INFO, "wepsrun file does not exist, creating it.");
+                            String wepsrunFile = "weps.run";
+                            WepsRunFileGenerator.GenerateWepsRunFile(wmr, sessionWorkDir, wepsrunFile);
 
-                    // run WEPS model
-                    try {
-                        // Always generate Weps Run file from JSON parameters (it is never provided in the model srvc signature)
-                        LOG.log(Level.INFO, "wepsrun file does not exist, creating it.");
-                        String wepsrunFile = "weps.run";
-                        WepsRunFileGenerator.GenerateWepsRunFile(wmr, sessionWorkDir, wepsrunFile);
+                            // Our WEPS model call uses the following cmd line args
+                            // /weps -W1 -u0 -I2 -t1 -P./ >stdout.txt 2>stderr.txt
+                            LOG.info("starting process");
+                            pc = new ProcessComponent();
 
-                        // Our WEPS model call uses the following cmd line args
-                        // /weps -W1 -u0 -I2 -t1 -P./ >stdout.txt 2>stderr.txt
-                        LOG.info("starting process");
-                        pc = new ProcessComponent();
-  
-                        // linux based WEPS
-                        pc.exe = Binaries.unpackResource("/bin/" + Binaries.getArch() + "/weps", new File(binDir)).toString();
-                        pc.args = new String[]{"-W1", "-u0", "-I2", "-t1", "-T1", "-P./"};
-                        pc.working_dir = sessionWorkDir;
-                        pc.execute();
+                            // linux based WEPS
+                            pc.exe = Binaries.unpackResource("/bin/" + Binaries.getArch() + "/weps", new File(binDir)).toString();
+                            pc.args = new String[]{"-W1", "-u0", "-I2", "-t1", "-T1", "-P./"};
+                            pc.working_dir = sessionWorkDir;
+                            pc.execute();
 
-                        // windows 32-bit WEPS - WARNING Runs much slower
-//                        pc.exe = Binaries.unpackResource("/bin/win-x86/weps.exe", new File(binDir)).toString();
-//                        File wepssh = new File(sessionWorkDir + "/runweps.sh");
-//                        String invokeWeps = "wine " + pc.exe + " -W1 -u0 -I2 -t1 -T1 -P./";
-//                        FileUtils.writeStringToFile(wepssh, invokeWeps);
-//                        wepssh.setExecutable(true);
-//
-//                        pc.working_dir = sessionWorkDir;
-//                        pc.exe = "./runweps.sh";
-//                        pc.args = new String[]{};  
-//                        pc.execute();
-                        
-                        // Check standard out for "inpsub" error - occurs when no soil file is available
-                        if (pc.stdout.contains("inpsub error"))
-                            throw new Exception("ERROR RUNNING WEPS--Standard output:" + pc.stdout);
-                        
-                        FileUtils.write(new File(workDir, "stdout.txt"), pc.stdout);
-                        FileUtils.write(new File(workDir, "stderr.txt"), pc.stderr);
+                            // windows 32-bit WEPS - WARNING Runs much slower
+    //                        pc.exe = Binaries.unpackResource("/bin/win-x86/weps.exe", new File(binDir)).toString();
+    //                        File wepssh = new File(sessionWorkDir + "/runweps.sh");
+    //                        String invokeWeps = "wine " + pc.exe + " -W1 -u0 -I2 -t1 -T1 -P./";
+    //                        FileUtils.writeStringToFile(wepssh, invokeWeps);
+    //                        wepssh.setExecutable(true);
+    //
+    //                        pc.working_dir = sessionWorkDir;
+    //                        pc.exe = "./runweps.sh";
+    //                        pc.args = new String[]{};  
+    //                        pc.execute();
 
-                        // Generate report output
-                        String reportJSON = Binaries.unpackResourceAbsolute("/bin/" + Binaries.getArch() + "/" + REPORT_JSON_FILENAME, sessionWorkDir + "/" + REPORT_JSON_FILENAME).toString();
-                    } catch (Exception e) {
-                        throw new ServiceException("WEPS error: error running WEPS model binary:" + e.toString());
+                            // Check standard out for "inpsub" error - occurs when no soil file is available
+                            if (pc.stdout.contains("inpsub error"))
+                                throw new Exception("ERROR RUNNING WEPS--Standard output:" + pc.stdout);
+
+                            FileUtils.write(new File(workDir, "stdout.txt"), pc.stdout);
+                            FileUtils.write(new File(workDir, "stderr.txt"), pc.stderr);
+
+                            // Generate report output
+                            String reportJSON = Binaries.unpackResourceAbsolute("/bin/" + Binaries.getArch() + "/" + REPORT_JSON_FILENAME, sessionWorkDir + "/" + REPORT_JSON_FILENAME).toString();
+                        } catch (Exception e) {
+                            throw new ServiceException("WEPS error: error running WEPS model binary:" + e.toString());
+                        }
+
+                        return pc.exitValue == 0 ? EXEC_OK : EXEC_FAILED;
+                    }else{
+                        return error;
                     }
-
-                    return pc.exitValue == 0 ? EXEC_OK : EXEC_FAILED;
                 } catch (Exception e) {
                     LOG.log(Level.SEVERE, "ERROR EXECUTING WEPS!!!");
 //                    LOG.log(Level.SEVERE, e.getStackTrace().toString());
@@ -278,7 +283,8 @@
     /*
      * Uses's Jim Lyon's LMOD to WEPS mgmt translator to create a WEPS mgmt file
      */
-    private void loadWepsMgmt() throws ServiceException {
+    private String loadWepsMgmt() throws ServiceException {
+        String error = null;
         try {
             LOG.info("WEPS MGMT exisitence check=" + JSONUtils.checkKeyExistsB(getParamMap(), WEPS_KEY_MANAGEMENT));
             if (JSONUtils.checkKeyExistsB(getParamMap(), WEPS_KEY_MANAGEMENT)) {
@@ -320,18 +326,16 @@
                     LOG.log(Level.INFO, "JSON to translate=" + translator.getInputJSONObject());
                 }
                 try {
-                    String error = translator.Translate();
+                    error = translator.Translate();
                     if(error != null)
                     {
                         try{
                         if (Integer.parseInt(error) != 1)
                         {
                             LOG.log(Level.SEVERE, "THERE IS AN ERROR CODE RETURNED FROM THE MAN2WEPS TRANSLATOR. Code=" + error);
-                            throw new ServiceException(error);
                         }
                         }catch(NumberFormatException ex){ // I want to transition the numeric errors to string errors like this
                             LOG.log(Level.SEVERE, "THERE IS AN ERROR: " + error);
-                            throw new ServiceException(error);
                         }
                     }
                 } catch (Exception e) {
@@ -348,10 +352,12 @@
                     throw new ServiceException("LMOD management translation error executing WEPS");
                     //return WEPS_ERROR_LMOD_TRANSLATION_ERROR;
                 }
+                if(error != null){
                 wmr.setRunCycle(Integer.toString(translator.getRotationYears()));
                 simulationYears = Integer.parseInt(wmr.getRunCycle()) * NRCS_CYCLE_COUNT;
                 wmr.setNumYears(Integer.toString(simulationYears));
                 wmr.setMgmtFile(WEPS_MGMT);
+                }
             }
         } catch (Exception e) {
             LOG.log(Level.SEVERE, "STACK TRACE FROM LMOD TRANSLATOR ERROR:");
@@ -360,6 +366,7 @@
             }
             throw new ServiceException("WEPS error: error translating WEPS mgmt from LMOD.");
         }
+        return error;
     }
 
     private void getFieldGeometry() throws ServiceException {