| 1 |
#!/usr/bin/env python |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
# Copyright (C) 2021 Even Rouault |
| 4 |
|
| 5 |
# One-time script to extract GRIB2 tables from degrib metaname.c[pp] file |
| 6 |
|
| 7 |
import shutil |
| 8 |
|
| 9 |
lines = open("degrib/metaname.cpp", "rt", encoding="utf-8").readlines() |
| 10 |
lines = [l[0:-1] for l in lines] |
| 11 |
|
| 12 |
|
| 13 |
def get_utf8(l): |
| 14 |
l = l.replace('\\xC3\\xA9" "', "é") |
| 15 |
l = l.replace('\\xC3\\xB6" "', "ö") |
| 16 |
l = l.replace('\\xC3\\xBC" "', "ü") |
| 17 |
l = l.replace('\\xC3\\xA4" "', "ä") |
| 18 |
assert "\\x" not in l, l |
| 19 |
return l |
| 20 |
|
| 21 |
|
| 22 |
def format_csv_line(l): |
| 23 |
out = "" |
| 24 |
in_quotes = False |
| 25 |
for c in l: |
| 26 |
assert c != "\\" |
| 27 |
if c == '"': |
| 28 |
out += c |
| 29 |
in_quotes = not in_quotes |
| 30 |
elif c != " " or in_quotes: |
| 31 |
out += c |
| 32 |
return out |
| 33 |
|
| 34 |
|
| 35 |
def strip_comments(l): |
| 36 |
out = "" |
| 37 |
pos = 0 |
| 38 |
while True: |
| 39 |
pos1 = l.find("/*", pos) |
| 40 |
if pos1 < 0: |
| 41 |
out += l[pos:] |
| 42 |
break |
| 43 |
else: |
| 44 |
out += l[pos:pos1] |
| 45 |
pos2 = l.find("*/", pos1) |
| 46 |
assert pos2 > 0, l |
| 47 |
pos = pos2 + 2 |
| 48 |
pos = out.find("//") |
| 49 |
if pos >= 0: |
| 50 |
out = out[0:pos] |
| 51 |
return out |
| 52 |
|
| 53 |
|
| 54 |
def extract_table(lines, out, start_pattern, add_code=False): |
| 55 |
found = False |
| 56 |
code = 0 |
| 57 |
line_idx = 0 |
| 58 |
line_count = len(lines) |
| 59 |
while line_idx < line_count: |
| 60 |
l = lines[line_idx] |
| 61 |
line_idx += 1 |
| 62 |
l = get_utf8(l) |
| 63 |
if start_pattern in l: |
| 64 |
found = True |
| 65 |
elif found: |
| 66 |
while "/*" in l and "*/" not in l: |
| 67 |
l += lines[line_idx] |
| 68 |
line_idx += 1 |
| 69 |
l = strip_comments(l).strip() |
| 70 |
if len(l) == 0: |
| 71 |
continue |
| 72 |
if "};" in l: |
| 73 |
break |
| 74 |
|
| 75 |
while l.endswith('"'): |
| 76 |
next_l = lines[line_idx] |
| 77 |
line_idx += 1 |
| 78 |
next_l = strip_comments(next_l).strip() |
| 79 |
assert next_l.startswith('"') |
| 80 |
l = l[0:-1] + next_l[1:] |
| 81 |
|
| 82 |
while not l.endswith("},") and l.endswith(","): |
| 83 |
next_l = lines[line_idx] |
| 84 |
line_idx += 1 |
| 85 |
next_l = strip_comments(next_l).strip() |
| 86 |
assert next_l.endswith("},") or next_l.endswith("}"), next_l |
| 87 |
l += next_l |
| 88 |
|
| 89 |
pos = l.find("}, {") |
| 90 |
if pos > 0: |
| 91 |
sublines = [l[0 : pos + 2]] |
| 92 |
l = l[pos + 3 :] |
| 93 |
while True: |
| 94 |
pos = l.find("}, {") |
| 95 |
if pos > 0: |
| 96 |
sublines.append(l[0 : pos + 2]) |
| 97 |
l = l[pos + 3 :] |
| 98 |
else: |
| 99 |
sublines.append(l) |
| 100 |
break |
| 101 |
else: |
| 102 |
sublines = [l] |
| 103 |
for l in sublines: |
| 104 |
assert l.startswith("{"), l |
| 105 |
l = l[1:] |
| 106 |
if l.endswith("},"): |
| 107 |
l = l[0:-2] |
| 108 |
elif l.endswith("}"): |
| 109 |
l = l[0:-1] |
| 110 |
else: |
| 111 |
assert False, l |
| 112 |
assert "{" not in l, l |
| 113 |
assert "}" not in l, l |
| 114 |
l = format_csv_line(l) |
| 115 |
if add_code: |
| 116 |
out.write("%d," % code) |
| 117 |
code += 1 |
| 118 |
out.write(l + "\n") |
| 119 |
|
| 120 |
|
| 121 |
def extract_center(): |
| 122 |
out = open("grib2_center.csv", "wt", encoding="utf-8") |
| 123 |
out.write("code,name\n") |
| 124 |
extract_table(lines, out, "Center[]") |
| 125 |
out.close() |
| 126 |
|
| 127 |
|
| 128 |
extract_center() |
| 129 |
|
| 130 |
|
| 131 |
def extract_subcenter(): |
| 132 |
out = open("grib2_subcenter.csv", "wt", encoding="utf-8") |
| 133 |
out.write("center_code,subcenter_code,name\n") |
| 134 |
extract_table(lines, out, "SubCenter[]") |
| 135 |
out.close() |
| 136 |
|
| 137 |
|
| 138 |
extract_subcenter() |
| 139 |
|
| 140 |
|
| 141 |
def extract_process(): |
| 142 |
out = open("grib2_process.csv", "wt", encoding="utf-8") |
| 143 |
out.write("center_code,process_code,name\n") |
| 144 |
extract_table(lines, out, "Process[]") |
| 145 |
out.close() |
| 146 |
|
| 147 |
|
| 148 |
extract_process() |
| 149 |
|
| 150 |
|
| 151 |
def extract_table_4_2(filename_suffix, table_name_pattern): |
| 152 |
out = open("grib2_table_4_2_%s.csv" % filename_suffix, "wt", encoding="utf-8") |
| 153 |
out.write("subcat,short_name,name,unit,unit_conv\n") |
| 154 |
extract_table(lines, out, table_name_pattern, add_code=True) |
| 155 |
out.close() |
| 156 |
|
| 157 |
|
| 158 |
extract_table_4_2("0_0", "MeteoTemp[]") |
| 159 |
extract_table_4_2("0_1", "MeteoMoist[]") |
| 160 |
extract_table_4_2("0_2", "MeteoMoment[]") |
| 161 |
extract_table_4_2("0_3", "MeteoMass[]") |
| 162 |
extract_table_4_2("0_4", "MeteoShortRadiate[]") |
| 163 |
extract_table_4_2("0_5", "MeteoLongRadiate[]") |
| 164 |
extract_table_4_2("0_6", "MeteoCloud[]") |
| 165 |
extract_table_4_2("0_7", "MeteoStability[]") |
| 166 |
extract_table_4_2("0_13", "MeteoAerosols[]") |
| 167 |
extract_table_4_2("0_14", "MeteoGases[]") |
| 168 |
extract_table_4_2("0_15", "MeteoRadar[]") |
| 169 |
extract_table_4_2("0_16", "MeteoRadarImagery[]") |
| 170 |
extract_table_4_2("0_17", "MeteoElectro[]") |
| 171 |
extract_table_4_2("0_18", "MeteoNuclear[]") |
| 172 |
extract_table_4_2("0_19", "MeteoAtmos[]") |
| 173 |
extract_table_4_2("0_20", "MeteoAtmoChem[]") |
| 174 |
extract_table_4_2("0_190", "MeteoText[]") |
| 175 |
# DEGRIB has the following non standard 253 code |
| 176 |
shutil.copy( |
| 177 |
"grib2_table_4_2_0_190.csv", "grib2_table_4_2_0_253.csv" # CCITT IA5 string |
| 178 |
) # METEO_CCITT2 |
| 179 |
extract_table_4_2("0_191", "MeteoMisc[]") |
| 180 |
extract_table_4_2("1_0", "HydroBasic[]") |
| 181 |
extract_table_4_2("1_1", "HydroProb[]") |
| 182 |
extract_table_4_2("2_0", "LandVeg[]") |
| 183 |
extract_table_4_2("2_3", "LandSoil[]") |
| 184 |
extract_table_4_2("3_0", "SpaceImage[]") |
| 185 |
extract_table_4_2("3_1", "SpaceQuantitative[]") |
| 186 |
extract_table_4_2("10_0", "OceanWaves[]") |
| 187 |
extract_table_4_2("10_1", "OceanCurrents[]") |
| 188 |
extract_table_4_2("10_2", "OceanIce[]") |
| 189 |
extract_table_4_2("10_3", "OceanSurface[]") |
| 190 |
extract_table_4_2("10_4", "OceanSubSurface[]") |
| 191 |
extract_table_4_2("10_191", "OceanMisc[]") |
| 192 |
|
| 193 |
|
| 194 |
def extract_NDFD_Override(): |
| 195 |
out = open("grib2_NDFD_Override.csv", "wt", encoding="utf-8") |
| 196 |
out.write("grib2_name,NDFD_name\n") |
| 197 |
extract_table(lines, out, "NDFD_Override[]") |
| 198 |
out.close() |
| 199 |
|
| 200 |
|
| 201 |
extract_NDFD_Override() |
| 202 |
|
| 203 |
|
| 204 |
def extract_local_table(name): |
| 205 |
out = open("grib2_table_4_2_local_%s.csv" % name, "wt", encoding="utf-8") |
| 206 |
out.write("prod,cat,subcat,short_name,name,unit,unit_conv\n") |
| 207 |
extract_table(lines, out, "%s_LclTable[]" % name) |
| 208 |
out.close() |
| 209 |
|
| 210 |
|
| 211 |
extract_local_table("NDFD") |
| 212 |
extract_local_table("HPC") |
| 213 |
extract_local_table("Canada") |
| 214 |
extract_local_table("MRMS") |
| 215 |
extract_local_table("NCEP") |