Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.  
  4. Generates normals report.
  5.  
  6. .DESCRIPTION
  7.  
  8. Processes all the .csv files in the nominated folder.  InputFolder is a required parameter.
  9.  
  10. .PARAMETER InputFolder
  11.  
  12. Location holding all the .csv files to process
  13.  
  14. .PARAMETER OutputFile
  15.  
  16. Filtered CSV file
  17.  
  18. .PARAMETER StartYear
  19.  
  20. First year to examine
  21.  
  22. .PARAMETER EndYear
  23.  
  24. Last year to examine
  25.  
  26. .PARAMETER help
  27.  
  28. Display help info
  29.  
  30. #>
  31.  
  32.  
  33. param (
  34.     [switch] $help,
  35.     [string] $InputFolder,
  36.     [string] $OutputFile = "",
  37.     [int]    $StartYear = 1951,
  38.     [int]    $EndYear = 1980
  39. )
  40.  
  41. if ($help -or (-not $InputFolder))
  42. {
  43.     Get-Help $MyInvocation.MyCommand.Definition
  44.     return
  45. }
  46.  
  47. if (-not (Test-Path $InputFolder -PathType Container))
  48.  
  49. {
  50.     $(throw "$InputFolder does not exist or is a file.")
  51. }
  52.  
  53. if (-not $OutputFile)
  54. {
  55.     $s = Split-Path -Path $InputFolder -Leaf
  56.     $OutputFile = [System.IO.Path]::ChangeExtension($s, ".normals.txt")
  57. }
  58.  
  59. New-Item -Force -Type file $OutputFile | Out-Null
  60.  
  61. $csvfiles = $InputFolder + "\*.csv"
  62.  
  63. $dataRecords = @()
  64.  
  65. # scan each file
  66. Get-ChildItem $csvfiles | Foreach-Object {
  67.   # set initial state for each file
  68.   $accumulator = @(0,0,0,0,0,0,0,0,0,0,0,0)
  69.   $count = @(0,0,0,0,0,0,0,0,0,0,0,0)
  70.   $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)
  71.   $parseState = 0
  72.  
  73.   # scan each line in the file
  74.   Get-Content $_ | Foreach-Object {
  75.       $parts      = $_.split(",")
  76.      
  77.       # Initial parse state -- Header up to Climate Identifier    
  78.       if ($parseState -eq 0) {
  79.         if ($parts[0] -eq "`"Climate Identifier`"") {
  80.             $ClimateIdentifier = $parts[1]
  81.             $parseState = 1
  82.         }
  83.       } # $parseState -eq 0
  84.      
  85.       # Final parse state -- Body processing
  86.       if ($parseState -eq 2) {
  87.      
  88.         $year =        $parts[1].Trim("`"")
  89.         $month =       $parts[2].Trim("`"")
  90.         $monthlyMean = $parts[5].Trim("`"")
  91.         $meanFlag =    $parts[6].Trim("`"")
  92.        
  93.         # process only data in our year range, where data is not missing/incomplete/null
  94.         if ( ($year -ge $StartYear) -and ($year -le $EndYear) -and
  95.            ($meanFlag -cne "I") -and ($meanFlag -cne "M") -and $monthlyMean) {
  96.           # increment counter and accumulate accumulator for the month
  97.           $count[$month - 1] += 1
  98.           $accumulator[$month - 1] += $monthlyMean
  99.         }
  100.       }
  101.  
  102.       # Intermediate parse state -- Skip rest of header and
  103.       # step state on hitting the header line
  104.       if ($parseState -eq 1) {
  105.         if ($parts[0] -eq "`"Date/Time`"") {
  106.             $parseState = 2
  107.         }
  108.       } # $parseState -eq 1
  109.      
  110.   } # all lines handled
  111.  
  112.   $line = "$ClimateIdentifier"
  113.   $index = 0
  114.   do {
  115.     if ($count[$index] -gt 0) {
  116.       $mean[$index] = $accumulator[$index]/$count[$index]
  117.     }
  118.    
  119.     $line += ",{0,5:n1}, {1,2:n0}" -f $mean[$index], $count[$index]
  120.     $index += 1
  121.   } while ($index -lt 12)
  122.   $dataRecords += $line
  123. } # all files
  124.  
  125. # More efficient than $array = $array | Sort-Object
  126. [Array]::Sort([array]$dataRecords)
  127. $dataRecords | Foreach-Object { Add-Content $OutputFile $_ }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement