IFCFile.java [tools/MetaModelTools/src/data/interpretors] Revision: ec5f4cade4553a8341e1cd241b111bfdb77a87a8  Date: Fri Jan 10 10:59:55 MST 2020
/*
 * 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 data.interpretors;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;

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

  private static final Logger LOGGER = Logger.getLogger(IFCFile.class.getName());
  //Logs for errors and warnings.

  String errorsInIfcFile = "";	// list of variables adjusted on output
  String mySavedFilename = null;

  public String localPhase = "local phase";
  public String taxOrder = "tax order";
  public int soilLossTolerance = 0;
  public double surfaceAlbedo = Double.NaN;
  public double surfaceSlope = -1;
  public double surfaceFragmentCover = 0;
  public double bedrockDepth = 9999;
  public double impermiableDepth = 9999;
  public double layerThickness[];
  public double fractionSand[];
  public double fractionSilt[];
  public double fractionClay[];
  public double fractionRock[];
  public double veryCoarseSandFraction[];
  public double coarseSandFraction[];
  public double mediumSandFraction[];
  public double fineSandFraction[];
  public double veryFineSandFraction[];
  public double wetBulkDensity[];
  public double organicMaterial[];
  public double soilpH[];
  public double calciumCarbonateEquivalent[];
  public double cationExchangeCapacity[];
  public double linearExtensibility[];
  public double aggregateMeanDiameter[];
  public double aggregateStdDeviation[];
  public double maxAggregateSize[];
  public double minAggregateSize[];
  public double aggregateDensity[];
  public double aggregateStability[];
  public double crustThickness = 0.01;
  public double crustDensity = Double.NaN;
  public double crustStability = Double.NaN;
  public double crustFraction = 0;
  public double crustLooseMaterialMass = 0;
  public double crustLooseMaterialFraction = 0;
  public double randomRoughness = 4.0;
  public double roughnessOrientation = 0;
  public double roughnessHeight = 0;
  public double roughnessSpacing = 10.0;
  public double roughnessWidth = 10.0;
  public double initialBulkDensity[];
  public double initialSWC[];
  public double saturatedSWC[];
  public double fieldCapacitySWC[];
  public double wiltingPointSWC[];
  public double soilCB[];
  public double airEntryPotential[];
  public double saturatedHydraulicConductivity[];
  public int numberOfSoilLayers;
  public double layerDepthBottom[];
  public double layerDepthTop[];
  public double dryBulkDensity[];
  public double tenthBarSWC[];
  public String state;
  public String county;
  public String soilSurveyAreaName;
  public String soilSurveyID;
  public String mapUnitSymbol;
  public String componentName;
  public String componentPercent;
  public String surfaceTexture;

  public IFCFile(String fileData) throws IOException {
    readIfc(fileData);
  }

  /**
   * ********************************************************************************************************
   */
  /* reads line from file skipping comments */
  private String readLine(BufferedReader inpf) {
    String line;
    try {
      line = inpf.readLine();
    } catch (IOException e) {
      return null;
    }
    if (line == null || line.length() == 0) {
      return null;
    }
    if (line.charAt(0) == '#') {
      line = readLine(inpf);
    }
    return line;
  }

  /**
   * ********************************************************************************************************
   */
  /* reads in a single double */
  private double readDouble(BufferedReader inpf) {
    String line = readLine(inpf);
    return Double.valueOf(line.trim()).doubleValue();
  }

  /**
   * ********************************************************************************************************
   */
  /* reads in a line of doubles */
  private double[] readDoubles(BufferedReader inpf, int numlay) {
    double rtnval[] = new double[numlay];
    String line = readLine(inpf);
    java.util.StringTokenizer st = new java.util.StringTokenizer(line, " ");
    for (int ldx = 0; ldx < numlay; ldx++) {
      String dblstr = st.nextToken();
      rtnval[ldx] = Double.valueOf(dblstr.trim()).doubleValue();
    }
    return rtnval;

  }

  /**
   * ********************************************************************************************************
   */
  /* parse Soil ID line */
  private void prsSoilID(String inplin, boolean newFlg) {
    if (!newFlg) {
      if (!inplin.startsWith("Soil ID ")) {
        return;
      }
      inplin = inplin.substring(8);
    }
    soilSurveyID = inplin.substring(0, inplin.indexOf('-'));
    inplin = inplin.substring(inplin.indexOf('-') + 1);
    mapUnitSymbol = inplin.substring(0, inplin.indexOf('-'));
    inplin = inplin.substring(inplin.indexOf('-') + 1);
    componentName = inplin.substring(0, inplin.indexOf('-'));
    inplin = inplin.substring(inplin.indexOf('-') + 1);
    componentPercent = inplin.substring(0, inplin.indexOf('-'));
    inplin = inplin.substring(inplin.indexOf('-') + 1);
    surfaceTexture = inplin.substring(0, inplin.indexOf('-'));
    inplin = inplin.substring(inplin.indexOf('-') + 1);
    state = inplin.substring(0, inplin.indexOf('-'));
    inplin = inplin.substring(inplin.indexOf('-') + 1);
    county = inplin.substring(0, inplin.indexOf('-'));
    inplin = inplin.substring(inplin.indexOf('-') + 1);
    soilSurveyAreaName = inplin.trim();
  }

  /**
   * ********************************************************************************************************
   */
  /* reads ifc file */
  private void readNewIFC(BufferedReader inpf) {
    try {
      int nsl;

      prsSoilID(readLine(inpf), true);

      localPhase = readLine(inpf);
      taxOrder = readLine(inpf);

      //	soilLossTolerance = Integer.valueOf(readLine(inpf).trim()).intValue();
      soilLossTolerance = Double.valueOf(readLine(inpf).trim()).intValue();
      //	soilLossTolerance = readDouble(inpf);
      surfaceAlbedo = readDouble(inpf);
      surfaceSlope = readDouble(inpf);
      surfaceFragmentCover = readDouble(inpf);

      bedrockDepth = readDouble(inpf);
      impermiableDepth = readDouble(inpf);

      nsl = Integer.valueOf(readLine(inpf).trim()).intValue();
      layerThickness = readDoubles(inpf, nsl);
      layerDepthTop = new double[nsl];
      layerDepthBottom = new double[nsl];

      fractionSand = readDoubles(inpf, nsl);
      fractionSilt = readDoubles(inpf, nsl);
      fractionClay = readDoubles(inpf, nsl);
      fractionRock = readDoubles(inpf, nsl);
      veryCoarseSandFraction = readDoubles(inpf, nsl);
      coarseSandFraction = readDoubles(inpf, nsl);
      mediumSandFraction = readDoubles(inpf, nsl);
      fineSandFraction = readDoubles(inpf, nsl);
      veryFineSandFraction = readDoubles(inpf, nsl);
      wetBulkDensity = readDoubles(inpf, nsl);
      //	dryBulkDensity = readDoubles(inpf, nsl);

      organicMaterial = readDoubles(inpf, nsl);
      soilpH = readDoubles(inpf, nsl);
      calciumCarbonateEquivalent = readDoubles(inpf, nsl);
      cationExchangeCapacity = readDoubles(inpf, nsl);
      linearExtensibility = readDoubles(inpf, nsl);

      aggregateMeanDiameter = readDoubles(inpf, nsl);
      aggregateStdDeviation = readDoubles(inpf, nsl);
      maxAggregateSize = readDoubles(inpf, nsl);
      minAggregateSize = readDoubles(inpf, nsl);
      aggregateDensity = readDoubles(inpf, nsl);
      aggregateStability = readDoubles(inpf, nsl);

      crustThickness = readDouble(inpf);
      crustDensity = readDouble(inpf);
      crustStability = readDouble(inpf);
      crustFraction = readDouble(inpf);
      crustLooseMaterialMass = readDouble(inpf);
      crustLooseMaterialFraction = readDouble(inpf);

      randomRoughness = readDouble(inpf);
      roughnessOrientation = readDouble(inpf);
      roughnessHeight = readDouble(inpf);
      roughnessSpacing = readDouble(inpf);
      roughnessWidth = readDouble(inpf);

      initialBulkDensity = readDoubles(inpf, nsl);
      initialSWC = readDoubles(inpf, nsl);
      saturatedSWC = readDoubles(inpf, nsl);
      fieldCapacitySWC = readDoubles(inpf, nsl);
      wiltingPointSWC = readDoubles(inpf, nsl);

      soilCB = readDoubles(inpf, nsl);
      airEntryPotential = readDoubles(inpf, nsl);
      saturatedHydraulicConductivity = readDoubles(inpf, nsl);

      //System.out.println("I_rIF:");
//            try {
//                while (true) {
//                    String inpLin = inpf.readLine();
//                    if (inpLin.charAt(0) != '#') {
//                        continue;
//                    }
//                    if (inpLin.trim().equals("# Notes:")) {
//                        continue;
//                    }
//                    errorsInIfcFile += inpLin.substring(1).trim() + '\n';
//                }
//            } catch (IOException e) {
//            }
    } finally {
      try {
        inpf.close();
      } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Unable to close soil input stream.", e);
      }
    }
    //System.out.println(errorsInIfcFile);
  }

  /**
   *
   * @param fileData
   * @throws java.io.IOException
   */
  /* reads ifc file */
  public void readIfc(String fileData) throws IOException {
    BufferedReader inpf;
    inpf = new BufferedReader(new StringReader(fileData));

    String firstLine = readLine(inpf);
    if (firstLine.startsWith("Version")) {
      readNewIFC(inpf);
    } else {
      throw new IOException("Invalid IFC File.  No 'Version' tag found");
    }
  }
}