-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoaa-seek.rb
71 lines (66 loc) · 1.49 KB
/
noaa-seek.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
# this file shows constant time using seek
# random access
# seek and read operate on bytes, NOT characters, so !! w/ multibyte char encoding
def get_week(file, week)
file.seek((week - 1) * 63)
week = {
date: file.read(10).strip,
temps: {}
}
[:nino12, :nino3, :nino34, :nino4].each do |region|
week[:temps][region] = {
temp: file.read(9).to_f,
change: file.read(4).to_f
}
end
week
end
File.open("data/wksst8110.for") do |file|
get_week(file, 3)
# irb(main):019:0> get_week(file, 3)
# {
# :date => "17JAN1990",
# :temps => {
# :nino12 => {
# :temp => 24.2,
# :change => -0.3
# },
# :nino3 => {
# :temp => 25.3,
# :change => -0.3
# },
# :nino34 => {
# :temp => 26.5,
# :change => -0.1
# },
# :nino4 => {
# :temp => 28.6,
# :change => 0.3
# }
# }
# }
get_week(file, 303) #...
get_week(file, 1303)
# irb(main):021:0> get_week(file, 1303)
# {
# :date => "17DEC2014",
# :temps => {
# :nino12 => {
# :temp => 22.9,
# :change => 0.1
# },
# :nino3 => {
# :temp => 26.0,
# :change => 0.8
# },
# :nino34 => {
# :temp => 27.4,
# :change => 0.8
# },
# :nino4 => {
# :temp => 29.4,
# :change => 1.0
# }
# }
# }
end