GenericIFCData.java [src/soils/utils] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package soils.utils;

import java.util.LinkedHashMap;
import java.util.function.Function;
import soils.Horizon;
import soils.exceptions.WEPSException;
import static soils.utils.SoilUtils.formatDouble;
import static soils.utils.SoilUtils.println;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class GenericIFCData {

    public static final int HZ1 = 0;
    public static final int HZ2 = 1;

    public static final String version = "Version: 1.0";
    public static final String soilId = "NA-NA-Organic Soil Mod-100-MUCK-NA-NA-NA";
    public static final String localPhase = "NA";
    public static final String soilOrder = "NA";  //taxorder

    public static final int soilLossTolerance = 3;
    public static final double drySoilAlbedo = 0.090;
    public static final double slopeGradient = 0.0;
    public static final double surfFragCover = 0.0;
    public static final int depthToBedrock = 99990;
    public static final int depthToRootRestrictingLayer = 99990;
    public static final int numLayers = 2;
    public static final int[] layerThk = {400, 1100};
    public static final double[] sandFraction = {0.5, 0.130};
    public static final double[] siltFraction = {0.4, 0.820};
    public static final double[] clayFraction = {0.1, 0.050};
    public static final double[] rockFragments = {0, 0};
    public static final double[] sandFractionVC = {0, 0};
    public static final double[] sandFractionC = {0.020, 0.003};
    public static final double[] sandFractionM = {0.128, 0.023};
    public static final double[] sandFractionF = {0.332, 0.065};
    public static final double[] sandFractionVF = {0.020, 0.039};
    public static final double[] bulkDensity = {0.390, 0.220};
    public static final double[] organicMatter = {0.7700, 0.8100};
    public static final double[] soilPh = {5.00, 5.00};
    public static final double[] CaCO3 = {0.00, 0.00};
    public static final double[] CEC = {40.00, 45.00};
    public static final double[] linearExtensibility = {0.000, 0.000};
    public static final double[] aggGMD = {5.000, 5.000};
    public static final double[] aggGSD = {5.000, 5.000};
    public static final double[] maxAggSize = {30.000, 30.000};
    public static final double[] minAggSize = {0.010, 0.010};
    public static final double[] aggDensity = {0.700, 0.700};
    public static final double[] aggStability = {4.000, 4.000};
    public static final double crustThk = 0.010;
    public static final double crustDensity = 0.600;
    public static final double crustStability = 4.00;
    public static final double crustSurfaceFraction = 0.00;
    public static final double massLooseMatCrust = 0.00;
    public static final double fractionLooseMatCrust = 0.00;
    public static final double randomRoughness = 4.00;
    public static final double ridgeOrientation = 0.00;
    public static final double ridgeHeight = 0.00;
    public static final double ridgeTopSpacing = 10.00;
    public static final double ridgeWidth = 10.00;
    public static final double[] initBulkDensity = {0.390, 0.220};
    public static final double[] initSoilWaterContent = {0.379, 0.379};
    public static final double[] satSoilWaterContent = {0.550, 0.550};
    public static final double[] fieldCapWaterContent = {0.335, 0.335};
    public static final double[] wiltingPointWaterContent = {0.240, 0.240};
    public static final double[] soilCBValue = {6.831, 6.831};
    public static final double[] airEntryPotential = {-4.379, -4.379};
    public static final double[] satHydraulicConductivity = {5.0E-5, 2.5E-5};

    public static final String DUMMY = "Dummy";
    public static final String STATE = "State";
    public static final String COUNTY = "County";
    public static final String SOIL_SURVEY_AREA_NAME = "Soil Survery Area Name";
    public static final String SOIL_SURVEY_ID = "Soil Survery ID";
    public static final String MUSYM = "Map Unit Symbol";
    public static final String COMP_NAME = "Component Name";
    public static final String COMP_PCT = "Component Percent";
    public static final String TAX_ORDER = "Soil Order";
    public static final String LOCAL_PHASE = "Local Phase";
    public static final String SURFACE_TEXTURE = "Surface texture";
    public static final String SOIL_LOSS_TOLERANCE = "Soil Loss Tolerance (tons/acre/year)";

    public static final String NUM_LAYERS = "Number of layers"; //1.0, 100.0), // 0
    public static final String LAYER_THICKNESS = "Layer thickness (mm)"; //0.0, 20000.0), // 1
    public static final String ORGANIC_MATTER = "Organic matter (kg/kg)"; //0.0, 1.0), // 2
    public static final String SAND_FRACTION = "Sand fraction"; //0.0, 1.0), // 3
    public static final String SILT_FRACTION = "Silt fraction"; //0.0, 1.0), // 4
    public static final String CLAY_FRACTION = "Clay fraction"; //0.0, 1.0), // 5
    public static final String ROCK_FRAGMENTS = "Rock fragments"; //0.0, 1.0), // 6
    public static final String SAND_FRACTION_VC = "Sand fraction very coarse"; //0.0, 1.0), // 7
    public static final String SAND_FRACTION_C = "Sand fraction coarse"; //0.0, 1.0), // 8
    public static final String SAND_FRACTION_M = "Sand fraction medium"; //0.0, 1.0), // 9
    public static final String SAND_FRACTION_F = "Sand fraction fine"; //0.0, 1.0), // 10
    public static final String SAND_FRACTION_VF = "Sand fraction very fine"; //0.0, 1.0), // 11
    public static final String BUILK_DENSITY = "Bulk Density (1/3 bar)(Mg/m^3)"; //0.1, 10.0), // 12
    public static final String INIT_BULK_DENSITY = "Initial Bulk Density (1/3 bar)(Mg/m^3)"; //0.1, 10.0), // 13
    public static final String AGG_GMD = "Aggregate geometric mean diameter (mm)"; //0.03, 30.0), // 14
    public static final String AGG_GSD = "Aggregate geometric standard deviation"; //1.0, 20.0), // 15
    public static final String MAX_AGG_SIZE = "Maximum aggregate size (mm)"; //1.0, 1000.0), // 16
    public static final String MIN_AGG_SIZE = "Minimum aggregate size (mm)"; //0.001, 5.0), // 17
    public static final String AGG_DENSITY = "Aggregate density (Mg/m^3)"; //0.6, 2.5), // 18
    public static final String AGG_STABILITY = "Aggregate stability (ln(J/m^2))"; //0.1, 7.0), // 19
    public static final String INIT_SOIL_WATER_CONTENT = "Initial soil water content (m^3/m^3)"; //0.011, 0.379), // 20
    public static final String SAT_SOIL_WATER_CONTENT = "Saturation soil water content (m^3/m^3)"; //0.208, 0.550), // 21
    public static final String FIELD_CAPACITY_WATER_CONTENT = "Field capacity water content (m^3/m^3)"; //0.012, 0.335), // 22
    public static final String WILTING_POINT_WATER_CONTENT = "Wilting point water content (m^3/m^3)"; //0.005, 0.242), // 23
    public static final String SOIL_CB_VALUE = "Soil CB value (exponent to Campbell's SWRC)"; //0.917, 27.027), // 24
    public static final String AIR_ENTRY_POTENTIAL = "Air entry potential (J/kg)"; //-17.91, 0.0), // 25
    public static final String SAT_HYDRAULIC_CONDUCTIVITY = "Saturated hydraulic conductivity (m/s)"; //0.0, 0.001), // 26
    public static final String SOIL_PH = "Soil PH (0-14)"; //0.0, 14.0), // 27
    public static final String CACO3 = "Calcium carbonate equivalent (CaCO3)"; //0.0, 1.0), // 28
    public static final String CATION_EXCHANGE_CAPACITY = "Cation exchange capacity (CEC) (meq/100g)"; //0.0, 400.0), // 29
    public static final String LINEAR_EXTENSIBILITY = "Linear extensibility"; //0.0, 30.0), // 30

    public static final String CRUST_THK = "Crust thickness (mm)"; //0.0, 23.0), // 1
    public static final String CRUST_DENSITY = "Crust density (Mg/m^3)"; //0.6, 2.0), // 2
    public static final String CRUST_STABILITY = "Crust stability (ln(J/m^2))"; //0.1, 7.0), // 3
    public static final String CRUST_SURFACE_FRACTION = "Crust surface fraction (m^2/m^2)"; //0.0, 1.0), // 4
    public static final String MASS_LOOSE_MAT_CRUST = "Mass of loose material on crust (kg/m^2)"; //0.0, 3.0), // 5
    public static final String FRACTION_LOOSE_MAT_CRUST = "Fraction of loose material on crust (m^2/m^2)"; //0.0, 1.0), // 6
    public static final String RANDOM_ROUGHNESS = "Random roughness (mm)"; //1.0, 30.0), // 7
    public static final String RIDGE_ORIENTATION = "Ridge orientation (deg)"; //0.0, 179.99), // 8
    public static final String RIDGE_HEIGHT = "Ridge height (mm)"; //0.0, 500.0), // 9
    public static final String RIDGE_TOP_SPACING = "Spacing between ridge tops (mm)"; //10.0, 2000.0), // 10
    public static final String RIDGE_WIDTH = "Ridge width (mm)"; //10.0, 4000.0), // 11
    public static final String DRY_SOIL_ALBEDO = "Dry soil albedo (fraction)"; //0.02, 1.0), // 12
    public static final String SLOPE_GRADIENT = "Slope gradient (fraction)"; //0.0, 0.999), // 13
    public static final String SURFACE_FRAGMENT_COVER = "Surface fragment cover or surface layer fragments (area fraction)"; //0.0, 0.999),// 14
    public static final String DEPTH_TO_BEDROCK = "Depth to bedrock (mm)"; //0.0, 99999), // 15
    public static final String DEPTH_TO_ROOT_RESTRICTING = "Depth to root restricting layer (mm)"; //0.0, 99999), // 16

    public static String formatIFCArray(String name, double[] defaultValues, boolean required, Horizon restrictingHorizon, int numdig, LinkedHashMap<String, Horizon> horizons, Function<Horizon, Double> getter) throws WEPSException {
        double value;
        String ret_val = println("# " + name);

        if ((null == defaultValues) || (defaultValues.length < 2)) {
            throw new WEPSException("Invalid default values passed to the IFC write function.");
        }

        for (Horizon horizon : horizons.values()) {
            value = getter.apply(horizon);
            if (horizon.useGenericHz1Values()) {
                value = defaultValues[HZ1];
            } else {
                if (horizon.useGenericHz2Values()) {
                    value = defaultValues[HZ2];
                }
            }
            if (EvalResult.testDefaultDouble(value)) {
                if ((required) && (horizon != restrictingHorizon)) {
                    throw new ArithmeticException("Soil property not found in one or more layers - " + name);
                } else {
                    ret_val += "     ";
                }
            } else {
                ret_val += formatDouble(value, numdig) + "     ";
            }
        }
        ret_val += println("");
        return ret_val;
    }

    public static String formatIFCValue(String nam, double val, int numdig, boolean required, double defaultVal) {
        String ret_val = println("# " + nam);
        if (EvalResult.testDefaultDouble(val)) {
            if (required) {
                throw new ArithmeticException("Soil property not found in one or more layers - " + nam);
            }else{
                ret_val += println(formatDouble(defaultVal, numdig));
            }
        } else {
            ret_val += println(formatDouble(val, numdig));
        }

        return ret_val;
    } 
    
    public static String formatIFCValue(String nam, double val, int numdig, boolean required) {
        String ret_val = println("# " + nam);
        if (EvalResult.testDefaultDouble(val)) {
            if (required) {
                throw new ArithmeticException("Soil property not found in one or more layers - " + nam);
            }else{
                ret_val += println("   ");
            }
        } else {
            ret_val += println(formatDouble(val, numdig));
        }

        return ret_val;
    }
}