SwmmReportParser.java [src/parsers] 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 parsers;

import SwmmReportObjects.LIDPerformanceSummary;
import SwmmReportObjects.OutfallLoadingSummary;
import SwmmReportObjects.SubcatchmentRunoffSummary;
import SwmmReportObjects.SwmmReport;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.io.FileUtils;
import utils.StringHelpers;

/**
 *
 * @author Lucas Yaege
 */
public class SwmmReportParser
{

    public File reportFile;
    private int lineIdx = 0;
    private List<String> lines;
    public SwmmReport report;
    private String hugeFile;

    public SwmmReportParser( File reportFile ) throws IOException
    {
        report = new SwmmReport();
        this.lineIdx = 0;
        this.reportFile = reportFile;
        lines = FileUtils.readLines( reportFile, StandardCharsets.UTF_8 );
        hugeFile = FileUtils.readFileToString( reportFile, StandardCharsets.UTF_8 );
        //Lets trim this up now so we don't have to put trims everywhere else.  #ThanksLouis!
        for ( int i = 0; i < lines.size(); i++ )
        {
            lines.set( i, lines.get( i ).trim() );
        }

    }

    public SwmmReport parse()
    {
        //Skip anything before Rainfall File Summary
        while ( !lines.get( lineIdx ).startsWith( "Subcatchment Runoff Summary" ) )
        {
            lineIdx++;
        }

        //Subcatchment Runoff bits (stop at next empty line)
        parseSubcatchmentRunoffSummary();
        if( hugeFile.contains( "LID Performance Summary" ) )
        {
            parseLIDPerformanceSummary();
        }
        parseOutfallLoadingSummary();
        return report;
    }

    private void parseSubcatchmentRunoffSummary()
    {
        lineIdx += 8; //skip the User readable bits
        while ( !lines.get( lineIdx ).isEmpty() )
        {
            String line = lines.get( lineIdx );
            String[] lineParts = StringHelpers.splitWithQuotes( lines.get( lineIdx ) );
            SubcatchmentRunoffSummary summaryLine = new SubcatchmentRunoffSummary();
            summaryLine.subcatchment = lineParts[0];
            summaryLine.totalPrecip = Double.parseDouble( lineParts[1] );
            summaryLine.totalRunon = Double.parseDouble( lineParts[2] );
            summaryLine.totalEvap = Double.parseDouble( lineParts[3] );
            summaryLine.totalInfil = Double.parseDouble( lineParts[4] );
            summaryLine.totalRunoffInches = Double.parseDouble( lineParts[7] );
            summaryLine.totalRunoffMG = Double.parseDouble( lineParts[8] );
            summaryLine.runoffCoeff = Double.parseDouble( lineParts[10] );

            this.report.runoffSummaries.add( summaryLine );
            lineIdx++;
        }
    }

    private void parseLIDPerformanceSummary()
    {

        while ( !lines.get( lineIdx ).startsWith( "LID Performance Summary" ) )
        {
            lineIdx++;
        }
        lineIdx += 8; //skip the User readable bits
        while ( !lines.get( lineIdx ).isEmpty() )
        {
            String[] lineParts = utils.StringHelpers.splitWithQuotes( lines.get( lineIdx ) );
            LIDPerformanceSummary LIDPerf = new LIDPerformanceSummary();
            LIDPerf.Subcatchment = lineParts[0];
            LIDPerf.LIDControl = lineParts[1];
            LIDPerf.TotalInflow = Double.parseDouble( lineParts[2] );
            LIDPerf.EvapLoss = Double.parseDouble( lineParts[3] );
            LIDPerf.InfilLoss = Double.parseDouble( lineParts[4] );
            LIDPerf.SurfaceOutflow = Double.parseDouble( lineParts[5] );
            LIDPerf.DrainOutflow = Double.parseDouble( lineParts[6] );
            LIDPerf.InitialStorage = Double.parseDouble( lineParts[7] );
            LIDPerf.FinalStorage = Double.parseDouble( lineParts[8] );
            LIDPerf.ContinuityError = Double.parseDouble( lineParts[9] );
            this.report.lidPerformanceSummaries.add( LIDPerf );
            lineIdx++;
        }
    }

    private void parseOutfallLoadingSummary()
    {
        while ( !lines.get( lineIdx ).startsWith( "Outfall Loading Summary" ) )
        {
            lineIdx++;
        }
        lineIdx += 8; //skip past the human readable header bits.

        while ( !lines.get( lineIdx ).startsWith( "---------------" ) )
        {
            String[] lineParts = utils.StringHelpers.splitWithQuotes( lines.get( lineIdx ) );
            OutfallLoadingSummary ols = new OutfallLoadingSummary();
            ols.outfall = lineParts[0];
            ols.flowFreqPcnt = Double.parseDouble( lineParts[1] );
            ols.avgFlowCFS = Double.parseDouble( lineParts[2] );
            ols.maxFlowCFS = Double.parseDouble( lineParts[3] );
            ols.totalVolumeMG = Double.parseDouble( lineParts[4] );

            this.report.outfallLoadingSummaries.add( ols );
            lineIdx++;

            System.out.println( ols.toString() );
        }

    }

}