-
Notifications
You must be signed in to change notification settings - Fork 279
/
gen_translations.py
216 lines (177 loc) · 7.33 KB
/
gen_translations.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
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
211
212
213
214
215
216
# Copyright Iris contributors
#
# This file is part of Iris and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
"""
Processing of metarelate metOcean content to provide Iris encodings of
metOcean mapping translations.
"""
from datetime import datetime
import os.path
import requests
import sys
import metarelate
from metarelate.fuseki import FusekiServer
from translator import (FORMAT_URIS, FieldcodeCFMappings, StashCFNameMappings,
StashCFHeightConstraintMappings,
CFFieldcodeMappings,
GRIB1LocalParamCFConstrainedMappings,
GRIB1LocalParamCFMappings, GRIB2ParamCFMappings,
CFConstrainedGRIB1LocalParamMappings,
CFGRIB2ParamMappings, CFGRIB1LocalParamMappings)
HEADER = """# Copyright {name} contributors
#
# This file is part of {name} and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
#
# DO NOT EDIT: AUTO-GENERATED
# Created on {datestamp} from
# http:https://www.metarelate.net/metOcean
# at commit {git_sha}
# https://github.com/metarelate/metOcean/commit/{git_sha}
{doc_string}
from collections import namedtuple
CFName = namedtuple('CFName', 'standard_name long_name units')
"""
HEADER_GRIB = """
DimensionCoordinate = namedtuple('DimensionCoordinate',
'standard_name units points')
G1LocalParam = namedtuple('G1LocalParam', 'edition t2version centre iParam')
G2Param = namedtuple('G2Param', 'edition discipline category number')
"""
DOC_STRING_GRIB = r'''"""
Provides GRIB/CF phenomenon translations.
"""'''
DOC_STRING_UM = r'''"""
Provides UM/CF phenomenon translations.
"""'''
YEAR = datetime.utcnow().year
def _retrieve_mappings(fuseki, source, target):
"""
Interrogate the metarelate triple store for all
phenomenon translation mappings from the source
scheme to the target scheme.
Args:
* fuseki:
The :class:`metrelate.fuseki.FusekiServer` instance.
* source:
The source metarelate metadata type for the mapping.
* target:
The target metarelate metadata type for the mapping.
Return:
The sequence of :class:`metarelate.Mapping`
instances.
"""
suri = 'http:https://www.metarelate.net/sparql/metOcean'
msg = 'Retrieving {!r} to {!r} mappings ...'
print(msg.format(source, target))
return fuseki.retrieve_mappings(source, target, service=suri)
def build_um_cf_map(fuseki, now, git_sha, base_dir):
"""
Encode the UM/CF phenomenon translation mappings
within the specified file.
Args:
* fuseki:
The :class:`metarelate.fuseki.FusekiServer` instance.
* now:
Time stamp to write into the file
* git_sha:
The git SHA1 of the metarelate commit
* base_dir:
The root directory of the Iris source.
"""
filename = os.path.join(base_dir, 'lib', 'iris', 'fileformats',
'um_cf_map.py')
# Create the base directory.
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
# Create the file to contain UM/CF translations.
with open(filename, 'w') as fh:
fh.write(HEADER.format(year=YEAR, doc_string=DOC_STRING_UM,
datestamp=now, git_sha=git_sha, name='Iris'))
fh.write('\n')
# Encode the relevant UM to CF translations.
maps = _retrieve_mappings(fuseki, FORMAT_URIS['umf'],
FORMAT_URIS['cff'])
# create the collections, then call lines on each one
# for thread safety during lines and encode
fccf = FieldcodeCFMappings(maps)
stcf = StashCFNameMappings(maps)
stcfhcon = StashCFHeightConstraintMappings(maps)
fh.writelines(fccf.lines(fuseki))
fh.writelines(stcf.lines(fuseki))
fh.writelines(stcfhcon.lines(fuseki))
# Encode the relevant CF to UM translations.
maps = _retrieve_mappings(fuseki, FORMAT_URIS['cff'],
FORMAT_URIS['umf'])
# create the collections, then call lines on each one
# for thread safety during lines and encode
cffc = CFFieldcodeMappings(maps)
fh.writelines(cffc.lines(fuseki))
def build_grib_cf_map(fuseki, now, git_sha, base_dir):
"""
Encode the GRIB/CF phenomenon translation mappings
within the specified file.
Args:
* fuseki:
The :class:`metarelate.fuseki.FusekiServer` instance.
* now:
Time stamp to write into the file
* git_sha:
The git SHA1 of the metarelate commit
* base_dir:
The root directory of the Iris source.
"""
filename = os.path.join(base_dir, 'lib', 'iris', 'fileformats',
'grib', '_grib_cf_map.py')
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
# Create the file to contain GRIB/CF translations.
with open(filename, 'w') as fh:
fh.write(HEADER.format(year=YEAR, doc_string=DOC_STRING_GRIB,
datestamp=now, git_sha=git_sha,
name='iris-grib'))
fh.write(HEADER_GRIB)
fh.write('\n')
# Encode the relevant GRIB to CF translations.
maps = _retrieve_mappings(fuseki, FORMAT_URIS['gribm'],
FORMAT_URIS['cff'])
# create the collections, then call lines on each one
# for thread safety during lines and encode
g1cfc = GRIB1LocalParamCFConstrainedMappings(maps)
g1c = GRIB1LocalParamCFMappings(maps)
g2c = GRIB2ParamCFMappings(maps)
fh.writelines(g1cfc.lines(fuseki))
fh.writelines(g1c.lines(fuseki))
fh.writelines(g2c.lines(fuseki))
# Encode the relevant CF to GRIB translations.
maps = _retrieve_mappings(fuseki, FORMAT_URIS['cff'],
FORMAT_URIS['gribm'])
# create the collections, then call lines on each one
# for thread safety during lines and encode
cfcg1 = CFConstrainedGRIB1LocalParamMappings(maps)
cg1 = CFGRIB1LocalParamMappings(maps)
cg2 = CFGRIB2ParamMappings(maps)
fh.writelines(cfcg1.lines(fuseki))
fh.writelines(cg1.lines(fuseki))
fh.writelines(cg2.lines(fuseki))
def main():
# Protect metarelate resource from 1.0 emergent bug
if not float(metarelate.__version__) >= 1.1:
raise ValueError("Please ensure that Metarelate Version is >= 1.1")
now = datetime.utcnow().strftime('%d %B %Y %H:%m')
git_sha = requests.get('http:https://www.metarelate.net/metOcean/latest_sha').text
gen_path = os.path.abspath(sys.modules['__main__'].__file__)
iris_path = os.path.dirname(os.path.dirname(gen_path))
with FusekiServer() as fuseki:
build_um_cf_map(fuseki, now, git_sha, iris_path)
build_grib_cf_map(fuseki, now, git_sha, iris_path)
if (git_sha !=
requests.get('http:https://www.metarelate.net/metOcean/latest_sha').text):
raise ValueError('The metarelate translation store has altered during'
'your retrieval, the results may not be stable.\n'
'Please rerun your retrieval.')
if __name__ == '__main__':
main()