forked from wmo-registers/code-manuals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufrtableb.py
168 lines (146 loc) · 6.51 KB
/
bufrtableb.py
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
from collections import namedtuple
from collections import OrderedDict
import glob
import os
import re
import shutil
from StringIO import StringIO
import urllib2
from zipfile import ZipFile
import cleanttl
from helpers import unit_of_measure
from ttlhead import ttlhead
wmo_url = 'http://www.wmo.int/pages/prog/www/WMOCodes/WMO306_vI2/LatestVERSION/BUFRCREX_22_0_1.zip'
cffile = 'BUFRCREX_22_0_1/BUFRCREX_22_0_1_TableB_en.txt'
# "No","ClassNo","ClassName_en","FXY","ElementName_en","Note_en","BUFR_Unit","BUFR_Scale","BUFR_ReferenceValue","BUFR_DataWidth_Bits","CREX_Unit","CREX_Scale","CREX_DataWidth_Char","Status"
# 1.00,"00","BUFR/CREX table entries","000001","Table A: entry",,"CCITT IA5","0","0","24","Character","0","3","Operational"
tbline = re.compile(r'(^[0-9]+?\.[0-9]+?),'
'("[0-9]+?"),'
'(".*?"|\B),'
'("[0-9]+?"|\B),'
'(".*?"|\B),'
'(".*?"|\B),'
'(".*?"|\B),'
'("-*[0-9]+?"|\B),'
'("-*[0-9]+? *"|\B),'
'(" *[0-9]+?"|\B),'
'(".*?"|\B),'
'("-*[0-9]+?"|\B),'
'("[0-9]+?"|\B),'
'("[a-zA-Z]*")')
def parsetb(line):
res = None
if not line.startswith('\xef\xbb\xbf"No",'):
matcher = tbline.match(line)
if matcher:
if not len(matcher.groups()) == 14:
raise ValueError('{}\nhas wrong number of elems'.format(line))
res = matcher.groups()
else:
import pdb; pdb.set_trace()
return res
def readfile():
lines = 0
entries = []
response = urllib2.urlopen(wmo_url)
zipfile = ZipFile(StringIO(response.read()))
for line in zipfile.open(cffile).readlines():
lines += 1
parsed = parsetb(line)
if parsed:
entries.append(parsed)
if len(entries) != lines-1:
raise ValueError('missing lines\n'
'{} lines, {} entries'.format(lines, len(entries)))
return entries
# "No","ClassNo","ClassName_en","FXY","ElementName_en",
# "Note_en","BUFR_Unit","BUFR_Scale","BUFR_ReferenceValue","BUFR_DataWidth_Bits",
# "CREX_Unit","CREX_Scale","CREX_DataWidth_Char","Status"
def make_collection(entries):
members = []
first_container = entries[0][1]
first_container = first_container.replace('"','')
collabel = '<{}>'.format(first_container)
elemstrs = []
for entry in entries:
container = entry[1].replace('"','')
codeval = entry[3].replace('"','')
test = (int(container), int(codeval))
urilabel = '<{}/{}>'.format(container, codeval[-3:])
elemstr = '{} a skos:Concept ; \n'.format(urilabel)
label = entry[4].replace('""', '``')
elemstr += '\trdfs:label {}@en ;\n'.format(label)
elemstr += '\tbufrcommon:fxy "{}" ;\n'.format(codeval)
elemstr += '\tskos:notation "{}" ;\n'.format(codeval[-3:])
if entry[5]:
elemstr += '\tskos:note {}@en ;\n'.format(entry[5])
unit = entry[6]
if not (unit.startswith('"CCITT IA5') or unit.startswith('"Character') or \
unit.startswith('"Code table') or unit.startswith('"Flag table')):
unit = unit_of_measure(unit.replace('"',''))
if unit.startswith('"Code table') or unit.startswith('"Flag table'):
elemstr += '\tdct:references <http://codes.wmo.int/bufr4/codeflag/{}-{}-{}> ;\n'.format(codeval[0], codeval[1:3], codeval[3:])
#unit = 'N_unit'
else:
elemstr += '\twmocommon:unit <http://codes.wmo.int/common/unit/{u}> ;\n'.format(u=unit)
elemstr += '\tbufrcommon:scale {} ;\n'.format(entry[7].replace('"',''))
elemstr += '\tbufrcommon:referenceValue {} ;\n'.format(entry[8].replace('"',''))
elemstr += '\tbufrcommon:dataWidth_Bits {} ;\n'.format(entry[9].replace('"',''))
# if entry[10]:
# unit = entry[10]
# if not (unit == '"CCITT IA5"' or unit == '"Character"' or unit == '"Code Table"'):
# unit = unit_of_measure(unit.replace('"',''))
# elemstr += '\tcrexcommon:unit <http://codes.wmo.int/common/c-6/{u}> ;\n'.format(u=unit)
# if entry[11]:
# elemstr += '\tcrexcommon:scale {} ;\n'.format(entry[11].replace('"',''))
# if entry[12]:
# elemstr += '\tcrexcommon:referenceValue {} ;\n'.format(entry[12].replace('"',''))
elemstr += '\t.\n'
if entry[13] == '"Operational"':
members.append(urilabel)
elemstrs.append(elemstr)
else:
raise ValueError('status not recognised\n{}'.format(entry))
colstr = '{} a skos:Collection ;\n'.format(collabel)
colstr += 'rdfs:label {}@en ;\n'.format(entries[0][2])
colstr += '\treg:owner <http://codes.wmo.int/system/organization/wmo> ;\n'
colstr += '\tdct:publisher <http://codes.wmo.int/system/organization/wmo> ;\n'
colstr += '\treg:manager <http://codes.wmo.int/system/organization/www-dm> ;\n'
colstr += '\tskos:member '
astr = ',\n\t\t'.join(members)
colstr += astr
colstr += '\t.\n\n'
with open('ttl/bufr4/b/bulk_{}.ttl'.format(entries[0][1].replace('"','')), 'w') as fhandle:
fhandle.write(ttlhead)
fhandle.write(colstr)
fhandle.write('\n'.join(elemstrs))
def makettl(entries):
classno = entries[0][1]
acol_entries = []
for entry in entries:
if not entry[1] == classno:
make_collection(acol_entries)
classno = entry[1]
acol_entries = []
acol_entries.append(entry)
def topfilewrite():
if not os.path.exists('ttl/bufr4'):
os.mkdir('ttl/bufr4')
##register declaration in bufrcommon
if not os.path.exists('ttl/bufr4/b'):
os.mkdir('ttl/bufr4/b')
with open('ttl/bufr4/b.ttl', 'w') as fhandle:
fhandle.write(ttlhead)
fhandle.write('<b> a reg:Register ;\n')
fhandle.write('\tdc:description "WMO No. 306 FM 94 BUFR (edition 4) Table B."@en ;\n')
fhandle.write('\treg:owner <http://codes.wmo.int/system/organization/wmo> ;\n')
fhandle.write('\tdct:publisher <http://codes.wmo.int/system/organization/wmo> ;\n')
fhandle.write('\treg:manager <http://codes.wmo.int/system/organization/www-dm> ;\n')
fhandle.write('\trdfs:label "BUFR4 table B"@en.\n')
def main():
topfilewrite()
entries = readfile()
makettl(entries)
if __name__ == '__main__':
cleanttl.clean()
main()