StratifiedTextures.java [tools/WepsSoilsIfcCreator/src/usda/weru/soil] Revision: 0963adc11caf5d7616128736008ebbc7f276df4c  Date: Wed Sep 02 15:16:53 MDT 2015
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package usda.weru.soil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class StratifiedTextures {
    private static final Logger LOGGER = Logger.getLogger(StratifiedTextures.class);
    private static Reference<StratifiedTextures> c_instance;
    private Map <String, double[]> c_values;
    private StratifiedTextures(){
        LOGGER.trace("created: " + toString());
        loadData();
    }

    public synchronized static StratifiedTextures getInstance(){

        StratifiedTextures temp = c_instance!= null ? c_instance.get() : null;
        if(temp == null){
            //need to create an instance
            temp = new StratifiedTextures();
            c_instance = new SoftReference<StratifiedTextures> (temp);
        }
        return temp;
    }

    private void loadData(){
        c_values = new HashMap<String, double[]>();
        InputStream in = StratifiedTextures.class.getResourceAsStream("StratifiedTextures.txt");
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            for(String line = reader.readLine(); line != null; line = reader.readLine()){
                line = line.trim();
                //skip comments and blank lines
                if(line.startsWith("#") ||line.length() == 0){
                    continue;
                }
                try{
                    String[] parts = line.split("\\|", -1);
                    assert parts.length == 5 : "Expected 5 fields: \"" + line + "\"";

                    String key = parts[0].trim().toUpperCase();
                    double s = Double.valueOf(parts[1].trim());    //sand
                    double vfs = Double.valueOf(parts[2].trim());  //very fine sand
                    double si = Double.valueOf(parts[3].trim());   //silt
                    double c = Double.valueOf(parts[4].trim());    //clay

                    double[] values = {s,vfs,si,c};

                    c_values.put(key, values);
                }
                catch(Exception e){
                    LOGGER.error("Unable to load StratifiedTextur record: \"" + line + "\"", e);
                }

            }
        }
        catch(Exception e){

        }
        finally{
            if(in != null){
                try{
                    in.close();
                }
                catch(Exception e){
                    LOGGER.error("Unable to close stream.", e);
                }
            }
        }
    }

    public String[] textures(){
        return c_values.keySet().toArray(new String[c_values.size()]);
    }

    public boolean isTextureValid(String texture){
        return c_values.containsKey(texture.toUpperCase());
    }

    
    
    private double getValue(String texture, int i){
        double[] values = c_values.get(texture.toUpperCase());
        if(values != null){
            return values[i];
        }
        else{
            return Double.NaN;
        }
    }
    
    public double getSand(String texture){
        return getValue(texture, 0);
    }

    public double getVeryFineSand(String texture){
        return getValue(texture, 1);
    }



    public double getSilt(String texture){
        return getValue(texture, 2);
    }

    public double getClay(String texture){
        return getValue(texture, 3);
    }



}