Skip to content

Commit

Permalink
Update create_master_lists.py
Browse files Browse the repository at this point in the history
as requested in :

- #219

>In GRIB2 Template definition files the octet number (from - to) for each entry in the GRIB section is given.
Most of the GRIB processing software packages need the length or number of octets for each entry, which has to be calculated from the specification of "OctetNo".
But with variables and repitition within some templates an automated calculation is sometimes not easy. (e.g. 37 + (ND-1)*4 + (NF-1)*4 -40 +(ND-1)*4 + (NF-1)*4)
Therefore, this is a proposal to add a column with the length of each entry in the GRIB2_Template files.
  • Loading branch information
antoinemerle authored Jan 14, 2025
1 parent f408d83 commit 5646f0d
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions scripts/create_master_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,28 @@ def close(self):
xmlfile.write(xml)


def parse_octet_length(octet_str):
# attempt to compute the length for simple cases
# fallback if expression is more complex
if any(sym in octet_str for sym in ['(', ')', '+', '*', 'ND', 'NF']):
return "Expression: " + octet_str

total_length = 0
parts = [p.strip() for p in octet_str.split(',')]
for p in parts:
range_match = re.match(r'^(\d+)-(\d+)$', p)
single_match = re.match(r'^(\d+)$', p)
if range_match:
start = int(range_match.group(1))
end = int(range_match.group(2))
total_length += (end - start + 1)
elif single_match:
total_length += 1
else:
return "Expression: " + octet_str
return str(total_length)


def process_files(files,pattern,writers,title_prefix):

rows=[]
Expand Down Expand Up @@ -141,6 +162,13 @@ def process_files(files,pattern,writers,title_prefix):
rows = [row for *_,row in decorated]



# For Template rows, add computed Length
if pattern == 'GRIB2_Template':
for row in rows:
octet_str = row.get("OctetNo", "")
row["Length"] = parse_octet_length(octet_str)

for row in rows:
for writer in writers:
writer.write_row(row)
Expand Down Expand Up @@ -176,13 +204,12 @@ def process_files(files,pattern,writers,title_prefix):
# Template tables
template_files = load_files("GRIB2_Template",basedir=".")

fieldnames=["Title_en","OctetNo","Contents_en","Note_en","noteIDs","codeTable","flagTable","Status"]
# Added "Length" to both fieldnames and xml_elements
fieldnames=["Title_en","OctetNo","Contents_en","Note_en","noteIDs","codeTable","flagTable","Length","Status"]
csv_writer = CSVWriter("txt/Template.txt",fieldnames)

xml_elements=["Title_en","OctetNo","Contents_en","Note_en","noteIDs","codeTable","flagTable","Status"]
xml_elements=["Title_en","OctetNo","Contents_en","Note_en","noteIDs","codeTable","flagTable","Length","Status"]
xml_writer = XMLWriter("xml/Template.xml",xml_elements,"GRIB2_Template_en")

writers = [csv_writer,xml_writer]
process_files(template_files,"GRIB2_Template",writers,"Identification template")


0 comments on commit 5646f0d

Please sign in to comment.