V1_0.java [src/java/soils/info] Revision: default  Date:
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package soils.info;

import csip.ModelDataService;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import javax.ws.rs.Path;
import org.codehaus.jettison.json.JSONArray;

/**
 *
 * @author david
 */
@Name("Daycent soil file reader")
@Description("Extracts information from soil file")
@Path("d/soilsinfo/1.0")
public class V1_0 extends ModelDataService {
    
JSONArray minDepth = new JSONArray();
JSONArray maxDepth = new JSONArray();
JSONArray bulkDensity = new JSONArray();
JSONArray fieldCapacity = new JSONArray();
JSONArray wiltingPoint = new JSONArray();
JSONArray evapCoef = new JSONArray();
JSONArray rootPercentage = new JSONArray();
JSONArray sandFraction = new JSONArray();
JSONArray clayFraction = new JSONArray();
JSONArray organicMatterFraction = new JSONArray();
JSONArray vswc = new JSONArray();
JSONArray satHydraulicCond = new JSONArray();
JSONArray pH = new JSONArray();
ArrayList<JSONArray> fields = new ArrayList(Arrays.asList(minDepth, maxDepth, bulkDensity, 
                fieldCapacity, wiltingPoint, evapCoef, rootPercentage, sandFraction,
                clayFraction, organicMatterFraction, vswc, satHydraulicCond, pH));
static final String[] fieldNames = new String[] {"minDepth", "maxDepth", "bulkDensity", "fieldCapacity", "wiltingPoint", "evapCoef", "rootPercentage",
            "evapCoef", "rootPercentage", "sandPercentage", "clayFraction", "organicMatterFraction", "vswc", "satHydraulicCond", "pH" };

int[] desiredColumns;
/*    
Column  1 - Minimum depth of soil layer (cm)
Column  2 - Maximum depth of soil layer (cm)
Column  3 - Bulk density of soil layer (g/cm^3)
Column  4 - Field capacity of soil layer, volumetric
Column  5 - Wilting point of soil layer, volumetric
Column  6 - Evaporation coefficient for soil layer (currently not being used)
Column  7 - Percentage of roots in soil layer, these values must sum to 1.0
Column  8 - Fraction of sand in soil layer, 0.0 - 1.0
Column  9 - Fraction of clay in soil layer, 0.0 - 1.0
Column 10 - Organic matter in soil layer, fraction 0.0 - 1.0
Column 11 - Minimum volumetric soil water content below wilting point for soil
            layer, soil water content will not be allowed to drop below this
            value
Column 12 - Saturated hydraulic conductivity of soil layer in centimeters per
            second
Column 13 - pH of soil layer
*/
  @Override
  protected void preProcess() throws Exception {
    
   
    String soilFileName = parameter().getString("soilFileName");
    desiredColumns = parameter().getIntArray("desiredColumns");
    File soilFile = workspace().getFile(soilFileName);
    try{
        FileInputStream fstream = new FileInputStream(soilFile);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null)   {
           String[] tokens = strLine.split("\\s+");
           for (int i = 0; i < tokens.length; i++) {
               fields.get(i).put(Double.parseDouble(tokens[i]));
           }
        }
        in.close();
    } catch (Exception e){
      System.err.println("Error reading soils file: " + e.getMessage());
    }
    
  }
  @Override 
  protected void doProcess() throws Exception {
      for (int col: desiredColumns ) {
           results().put(fieldNames[col], fields.get(col), "Data for column " + fieldNames[col]);
      }
  }
}