-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufr2amdar.rb
210 lines (178 loc) · 4.65 KB
/
bufr2amdar.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/ruby
$LOAD_PATH.push File.dirname($0) ##del
require 'bufrdump' ##del
require 'output' ##del
class Bufr2amdar
def initialize out
@out = out
@hdr = @reftime = nil
@ahl_is_set = nil
end
def newbufr hdr
@hdr = hdr
rt = @hdr[:reftime]
@reftime = Time.gm(rt.year, rt.mon, rt.day, rt.hour)
gg = (rt.hour + 2) / 3 * 3
@reftime += (3600 * (gg - rt.hour))
@ahl_is_set = false
end
# 出力ハンドラにヘッダを設定する。既にしていたらやらない。
def startmsg codename, yygg
return if @ahl_is_set
tt = 'UD' # Part A
yygggg = @reftime.strftime('%d%H00')
@out.startmsg(tt, yygggg, @hdr)
@out.print_fold([codename, yygg])
@ahl_is_set = true
end
# 記述子 fxy を含む最初の記述子-値ペアを返す
def find tree, fxy
for elem in tree
if elem.first == fxy then
return elem[1]
elsif Array === elem.first
r = find(elem.first, fxy)
return r if r
end
end
return nil
end
# 記述子 fxy を含む最初の記述子の *直前* の記述子-値ペアを返す
def find_prev tree, fxy
tree.size.times {|i|
if tree[i].first == fxy then
return tree[i-1][1]
end
}
return nil
end
def itoa1 ival
case ival when 0..9 then format('%01u', ival) else '/' end
end
def itoa2 ival
case ival when 0..99 then format('%02u', ival) else '//' end
end
def itoa3 ival
case ival when 0..999 then format('%03u', ival) else '///' end
end
def itoa4 ival
case ival when 0..9999 then format('%04u', ival) else '////' end
end
def subset tree
report = []
#--- Section 1 ---
# AMDAR YYGG
yy = find(tree, '004003') || @reftime.day
_GG = find(tree, '004004') || @reftime.hour
yygg = [itoa2(yy), itoa2(_GG)].join
startmsg('AMDAR', yygg)
#--- Section 2 ---
# ipipip
ip3 = case find(tree, '008009')
when 0..2 then 'UNS'
when 3 then 'LVR'
when 4 then 'LVW'
when 5, 7..10 then 'ASC'
when 6, 11..14 then 'DSC'
else '///'
end
report.push ip3
# IA...IA
_IA = find(tree, '001008').to_s.strip
_IA = 'AMDAR' if _IA.empty?
report.push _IA
# LaLaLaLaA LoLoLoLoLoB
lat = find(tree, '005001')
lon = find(tree, '006001')
@out.tell_latlon(lat, lon)
latmin = (lat.abs * 60 + 0.5).floor
la12 = latmin / 60
la34 = latmin % 60
latsign = (lat >= 0.0) ? 'N' : 'S'
report.push [itoa2(la12), itoa2(la34), latsign].join
lonmin = (lon.abs * 60 + 0.5).floor
lo123 = lonmin / 60
lo45 = lonmin % 60
lonsign = (lon >= 0.0) ? 'E' : 'W'
report.push [itoa3(lo123), itoa2(lo45), lonsign].join
gg = find(tree, '004005')
report.push [yygg, itoa2(gg)].join
# ShhIhIhI
pheight = find(tree, '007010')
if pheight.nil? then
pheight = find(tree, '007002')
end
if pheight then
pheight *= 3.280839895
sh = (pheight >= 0) ? 'F' : 'A'
hihihi = (pheight + 50) / 100
report.push [sh, itoa3(hihihi)].join
end
# SSTATATA
t = find(tree, '012101')
if t.nil? then
t = find(tree, '012001')
end
if t
t -= 273.15
ss = (t >= 0) ? 'PS' : 'MS'
t = (t.abs * 10 + 0.5).floor
report.push [ss, itoa3(t)].join
else
report.push 'PS///'
end
td = find(tree, '012103')
if td
td -= 273.15
sd = (td >= 0) ? 'PS' : 'MS'
td = (td.abs * 10 + 0.5).floor
report.push [sd, itoa3(td)].join
end
ddd = find(tree, '011001')
fff = find(tree, '011002')
fff = (fff * 1.9438445 + 0.5).floor if fff
report.push [itoa3(ddd), '/', itoa3(fff)].join if ddd or fff
tb = case find(tree, '011037')
when 0..1 then 0
when 2..9 then 1
when 10..20 then 2
when 21..27 then 3
else nil
end
report.push ['TB', itoa1(tb)].join
tqc = case find_prev(tree, '012101')
when 0 then 0
when 1 then 1
else nil
end
report.push ['S//', itoa1(tqc)].join if tqc
report.last.sub!(/$/, '=')
@out.print_fold(report)
end
def endbufr
return unless @ahl_is_set
@out.flush
@hdr = @reftime = nil
@ahl_is_set = nil
end
def close
@out.close
end
end
if $0 == __FILE__
db = BufrDB.setup
# コマンドラインオプションは最初の引数だけ
outopts = if /^-o/ =~ ARGV.first then ARGV.shift else '' end
encoder = Bufr2amdar.new(Output.new(outopts, db.path))
ARGV.each{|fnam|
BUFRScan.filescan(fnam){|bufrmsg|
next unless bufrmsg[:cat] == 4
case bufrmsg[:subcat]
when 0
else next
end
db.decode(bufrmsg, :direct, encoder)
}
}
encoder.close
end