forked from NRCan-IETS-CE-O-HBC/HTAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover-results.rb
132 lines (93 loc) · 2.71 KB
/
recover-results.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env ruby
# This script parses through the files
#use warnings;
#use strict;
require 'optparse'
require 'fileutils'
$gMasterPath = Dir.getwd()
$RemoteDir = "HousingModels/OptFramework";
$RemoteFile = "OutputListingAll.txt";
$OutputFile = "CloudResultsAllData.csv";
$Batch = 0;
$TotalRows = 0;
$help_msg = "
recover-results.rb - parses OutputListingAll.txt files and combines them.
usage: recover-results.rb [options]
-h, --help Displays help
-l, --local Search for OutputListingAll.txt in current (local) dirctory
"
# JTB: Remote use not yet implemented for HOT2000 use with Remote Desktop Connection:
# (e.g. recover-results.rb 54.204.133.55 )
if ARGV.empty? then
puts $help_msg
exit()
end
$local = false
optparse = OptionParser.new do |opts|
opts.banner = $help_msg
opts.on("-h", "--help", "Displays help") do
puts opts
exit
end
opts.on("-l", "--local", "Search for OutputListingAll.txt in current (local) dirctory") do
$local = true
end
end
optparse.parse!
if ($local)
FileUtils.rm Dir.glob('TempResultsBatch*.txt')
FileUtils.rm Dir.glob('CloudResultsAllData.csv')
begin
FileUtils.cp( $RemoteFile, "TempResultsBatch1.txt ")
rescue
puts "Can't find $RemoteFile ! \n"
end
end
FileList = Array.new
Dir.glob('TempResultsBatch*.txt').each do |f|
FileList.push(f)
end
Dir.glob('CloudResultsBatch*.txt').each do |f|
FileList.push(f)
end
#i = 0
batch = 0
index =0
header =""
data = ""
recoveredLines = 0
FileList.each do |fileName|
batch += 1
lineCount = 0
puts "Recovering results from " << fileName
contents = File.open(fileName, 'r')
contents.each do |line|
lineCount += 1
#puts "F: " << fileName <<" | " << batch.to_s << " | " << lineCount.to_s
# line = line.gsub!(/\s+/,", ")
line = line.gsub(/([^ ]) ([^ ])/,"\\1\\2")
line = line.gsub(/\s+/,", ")
# $line =~ s/\s+/,/g;
if lineCount < 20
#GenOpt preamble. DO nothing
elsif batch == 1 and lineCount == 20
header = "ID, batch, " << line.to_s << ", status\n"
elsif lineCount > 20
recoveredLines += 1
index += 1
data << index.to_s << ", " <<batch.to_s << ", " << line.to_s << "\n"
end
end
contents.close
end
puts "Recovered "<< recoveredLines.to_s << " lines from " << batch.to_s << " files.\n"
output = File.open('CloudResultsAllData.csv', 'a')
output.write(header)
output.write(data)
output.close
puts "Results written to file CloudResultsAllData.csv\n"
#puts header
#my @AllFiles = split /\s/, `ls CloudResultsBatch*.txt TempResultsBatch*.txt`;
exit()
FileUtils.rm Dir.glob('TempResultsBatch*.txt')
exit()