Monday, November 05, 2007

SED & eQuest SIM File Reports

Topic: Extracting individual reports from SIM files using SED

See this post for another SED vs. Awk comparative editorial. Following is my own take, after fiddling around with SED for a few hours in an attempt accomplish discrete value data extraction.

Next time: Discrete value data extraction using Awk!

SED = Sledgehammer

SED seems most useful for gross editing tasks -- i.e. global search-and-replacements or chunking out reports from the SIM file. SED is essentially batch application of regular expressions.

Lacking basic programming constructs, there are fundamental functionality limitations. But for the tasks it is capable performing, SED accomplishes them exceedingly well.

Awk = Sawzall

With built-in programming language constructs, Awk seems to be more suited for 'refined' tasks like discrete value data extraction, doing arithmetic on arrays and columns, or creating custom reports.

They both seem to be the right tool, just for different tasks. Following are some potentially useful SED examples (using GNU SED for Win32):

SED /HW-BOILER/!d SimFile.SIMOutputs from SimFile.SIM all lines with HW-BOILER
SED /PS-E/,/PS-F/!d SimFile.SIM
Outputs the PS-E report contained in SimFile.SIM
@ECHO OFF
SET SIMRPT=Energy Cost Summary
SET SIMBGN=BEPS
SET SIMEND=BEPU
SED.EXE --version >NUL
IF %ERRORLEVEL% GTR 0 GOTO USAGE
IF EXIST %1.SIM GOTO NEXT
GOTO USAGE

:NEXT
SED /%SIMBGN%/,/%SIMEND%/!d %1.SIM >%TEMP%\~SED.TMP
IF /I "%2" EQU "/A" GOTO APPEND
SED /%SIMEND%/d %TEMP%\~SED.TMP
GOTO :EOF
:APPEND
SED /%SIMEND%/d %TEMP%\~SED.TMP >>%0.RPT
GOTO :EOF

:USAGE
ECHO.
ECHO The %SIMBGN%.CMD script extracts the "%SIMRPT%" section from
ECHO the specified .SIM report using GNU SED for Windows:
ECHO.
ECHO http://gnuwin32.sourceforge.net/packages/sed.htm
ECHO.
ECHO Note that SED.EXE must exist in the shell path. Thus after installation
ECHO using the package installer, it is recommended that a copy of SED.EXE
ECHO be placed in the %WINDIR%\system32 directory.
ECHO.
ECHO Usage:
ECHO.
ECHO %SIMBGN% SimFile [/A]
ECHO.
ECHO ...where SimFile is specified without the .SIM extension and must exist.
ECHO If the optional /A parameter is supplied, the output will be appended to
ECHO a file named %SIMBGN%.RPT so that a 'FOR' command may collect
ECHO all %SIMBGN% reports into a single file for further processing.
ECHO.
ECHO Example:
ECHO.
ECHO FOR %%i in (*.SIM) DO %SIMBGN% "%%~ni" /A
ECHO.
ECHO CAUTION!!! The %SIMBGN%.RPT file must be deleted manually
ECHO to be refreshed, else updates will simply be appended to it.
ECHO.
ECHO Released to public domain under General Public Licence (GPL)
ECHO http://www.gnu.org/licenses/gpl.html
ECHO Copyright 2007 by Brandon Nichols, PE
ECHO.

The command script at left extracts the BEPS report from the specified .SIM file and echoes it to the screen by default.

Copy the code, paste it to a blank Notepad window. Save the file as 'BEPS.CMD' (be sure to change the drop-down in the Notepad file-save dialog from '*.txt' to 'All Files') to the eQuest reports directory, or to a designated script directory in the command path.

Assure that GNU SED.EXE may also be found in the command path -- see the installation tip under 'USAGE' in the script.

Type 'BEPS SimFile' at the CMD prompt, where SimFile is specified WITHOUT the .SIM extension.

Now for some real fun type 'BEPS' at the CMD prompt and hit return, then copy/paste the 'FOR' example displayed on the 'Usage' screen to the CMD prompt and hit return. Written to disk will be each BEPS report from all simulations in a directory into a text file named BEPS.RPT for output or further processing.

To create your own report extractor, simply change the three 'SET' values at the top of the file and save it under a new name.

For instance changing values of SIMRPT to Summary of Utility Rates,
SIMBGN to BEPU, and SIMEND to PS-H creates a BEPU extractor.


When creating a new report extractor, examine the contents of the SIM file or see this post to determine the actual report order -- its not the same as shown in the drop-down box in the SIM file viewer.

Perhaps not immediately obvious, but a 'System Report Extractor' for a report within the SIM file 'system repeat block' will extract each instance of the report for all systems! For example, copy the script above to Notepad and change the values of SIMRPT to 'System Utility Energy Use', SIMBGN to 'SS-H', and SIMEND to 'SS-I' and save to SS-H.CMD (be sure to change the drop-down in the Notepad file-save dialog from '*.txt' to 'All Files') in same directory where the SIM files are located. Thus created is an SS-H extractor that extracts all System Utility Energy Use reports for every system from the specified SIM file!.

Type 'SS-H SimFile' (without the .SIM extension) at the command line to test the output. Note that SimFile must exist in the same directory as the script and GNU SED.EXE must be in the command search path. If the above conditions have been met and your syntax is correct, then all SS-H reports will be echoed sequentially to the screen.

Now type just 'SS-H' at the command line and hit return. Then copy/paste the 'FOR' example from the usage screen to the command line and hit return. Congratulations, you have just created the text file SS-H.RPT, containing each System Utility Energy Use report for every system from all SIM files in the current directory!

No comments: