Skip to content

Instantly share code, notes, and snippets.

@alexrothenberg
Created March 21, 2011 20:14
Show Gist options
  • Save alexrothenberg/880125 to your computer and use it in GitHub Desktop.
Save alexrothenberg/880125 to your computer and use it in GitHub Desktop.
Parse WXDATA files to extract snowfall totals
def parse file_name
lines = File.readlines(file_name)
# puts "#{file_name} has #{lines.size} lines"
lines[4] =~ /.*: (\d+)/
data_id = $1.to_i
monthly_data(data_id, lines)
rescue => e
puts "Error with file #{file_name}: #{e}"
[data_id]
end
def snow_table_row(lines)
[40, 46].detect {|row| puts "'#{lines[row]}'"; lines[row] =~ /Snow Table/ }
end
def annual_data(data_id, lines)
raise 'No snow table' if snow_table_row(lines).nil?
annual_row = snow_table_row(lines) + 14
data = lines[annual_row].split ' '
raise 'Line #{annual_row} is not the ann row' unless data[0] == 'Ann'
data[0] = data_id
data
end
def monthly_data(data_id, lines)
raise 'No snow table' if snow_table_row(lines).nil?
jan_row = snow_table_row(lines) + 2
monthly_snow_data = lines[jan_row, 12]
monthly_snow = monthly_snow_data.map do |line|
line.split(' ')[1]
end
[data_id] + monthly_snow
end
def parse_all
Dir['wxdata/*.txt'].map do |file|
parse file
end
end
def wxdata
File.open('wxdata.csv', 'w') do |f|
data = parse_all
data.each do |row|
f.puts row.join("\t")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment