Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Generates normals report.
- .DESCRIPTION
- Processes all the .csv files in the nominated folder. InputFolder is a required parameter.
- .PARAMETER InputFolder
- Location holding all the .csv files to process
- .PARAMETER OutputFile
- Filtered CSV file
- .PARAMETER StartYear
- First year to examine
- .PARAMETER EndYear
- Last year to examine
- .PARAMETER help
- Display help info
- #>
- param (
- [switch] $help,
- [string] $InputFolder,
- [string] $OutputFile = "",
- [int] $StartYear = 1951,
- [int] $EndYear = 1980
- )
- if ($help -or (-not $InputFolder))
- {
- Get-Help $MyInvocation.MyCommand.Definition
- return
- }
- if (-not (Test-Path $InputFolder -PathType Container))
- {
- $(throw "$InputFolder does not exist or is a file.")
- }
- if (-not $OutputFile)
- {
- $s = Split-Path -Path $InputFolder -Leaf
- $OutputFile = [System.IO.Path]::ChangeExtension($s, ".normals.txt")
- }
- New-Item -Force -Type file $OutputFile | Out-Null
- $csvfiles = $InputFolder + "\*.csv"
- $dataRecords = @()
- # scan each file
- Get-ChildItem $csvfiles | Foreach-Object {
- # set initial state for each file
- $accumulator = @(0,0,0,0,0,0,0,0,0,0,0,0)
- $count = @(0,0,0,0,0,0,0,0,0,0,0,0)
- $mean = @(-99.9,-99.9,-99.9,-99.9,-99.9,-99.9,-99.9,-99.9,-99.9,-99.9,-99.9,-99.9)
- $parseState = 0
- # scan each line in the file
- Get-Content $_ | Foreach-Object {
- $parts = $_.split(",")
- # Initial parse state -- Header up to Climate Identifier
- if ($parseState -eq 0) {
- if ($parts[0] -eq "`"Climate Identifier`"") {
- $ClimateIdentifier = $parts[1]
- $parseState = 1
- }
- } # $parseState -eq 0
- # Final parse state -- Body processing
- if ($parseState -eq 2) {
- $year = $parts[1].Trim("`"")
- $month = $parts[2].Trim("`"")
- $monthlyMean = $parts[5].Trim("`"")
- $meanFlag = $parts[6].Trim("`"")
- # process only data in our year range, where data is not missing/incomplete/null
- if ( ($year -ge $StartYear) -and ($year -le $EndYear) -and
- ($meanFlag -cne "I") -and ($meanFlag -cne "M") -and $monthlyMean) {
- # increment counter and accumulate accumulator for the month
- $count[$month - 1] += 1
- $accumulator[$month - 1] += $monthlyMean
- }
- }
- # Intermediate parse state -- Skip rest of header and
- # step state on hitting the header line
- if ($parseState -eq 1) {
- if ($parts[0] -eq "`"Date/Time`"") {
- $parseState = 2
- }
- } # $parseState -eq 1
- } # all lines handled
- $line = "$ClimateIdentifier"
- $index = 0
- do {
- if ($count[$index] -gt 0) {
- $mean[$index] = $accumulator[$index]/$count[$index]
- }
- $line += ",{0,5:n1}, {1,2:n0}" -f $mean[$index], $count[$index]
- $index += 1
- } while ($index -lt 12)
- $dataRecords += $line
- } # all files
- # More efficient than $array = $array | Sort-Object
- [Array]::Sort([array]$dataRecords)
- $dataRecords | Foreach-Object { Add-Content $OutputFile $_ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement