Skip to content

Commit a594454

Browse files
author
Eric Lujan
committed
Added scripts
1 parent cbc29aa commit a594454

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

script/noaafilter.rb

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'csv'
2+
3+
print "Enter the filename of the NOAA data to parse: "
4+
# Open the specified file into memory in a read-only mode
5+
filename = gets.chomp
6+
datafile = File.open(filename) or die "Could not open the file."
7+
# Create an empty array to store the lines in the file
8+
contents = []
9+
# Read each line into the contents array for processing
10+
datafile.each_line do |line|
11+
contents.push line
12+
end
13+
# Prompt the user for a new filename
14+
print "Enter the filename for the processed data (.csv will be appended): "
15+
newfilename = gets.chomp + ".csv"
16+
# Create the new file and open it for writing
17+
newfile = File.open(newfilename, "w")
18+
# Add the standard CSV headers to the file
19+
newfile.puts "date,status,pressure,height,temperature,dewpointdepression,winddirection,windspeed"
20+
# Prompt for the target altitude
21+
print "Enter the target altitude (m): "
22+
target = gets.to_i
23+
print "Enter the tolerance (m): "
24+
tolerance = gets.to_i
25+
@lower_bound = target - tolerance
26+
@upper_bound = target + tolerance
27+
# Let the user know this may take a while
28+
puts "Processing, reformatting, and filtering data file. This may take a while. \nWe'll let you know when we're done."
29+
# Iterate through each line and remove any that have invalid data
30+
contents.each do |line|
31+
# Handle a regular line of data
32+
if line[0] == "#"
33+
@year = line[6..9]
34+
@month = line[10..11]
35+
@day = line[12..13]
36+
end
37+
unless line.include? "-9999" or line[0] == "#"
38+
line = @month + '/' + @day + '/' + @year + ',' + line
39+
line = line.gsub(/ +/, ',')
40+
line = line.gsub(/B/, '')
41+
# Parse the newly created CSV data to extract its altitude
42+
csvdata = CSV.parse(line)
43+
csvdata.each do |row|
44+
@altitude = row[3].to_i
45+
end
46+
# Test to see if the altitude falls between the specified range. If so, put it in the file.
47+
if (@lower_bound..@upper_bound) === @altitude
48+
newfile.puts line
49+
end
50+
end
51+
# Handle an initialization header
52+
end
53+
# Clear up system resources, since we're just nice like that
54+
newfile.close
55+
datafile.close
56+
# Celebrate, perhaps by opening some champagne?
57+
puts "New file saved!"

0 commit comments

Comments
 (0)