Skip to content

Commit 654e414

Browse files
committed
add table 1
1 parent deaaab9 commit 654e414

File tree

2 files changed

+85
-9
lines changed

2 files changed

+85
-9
lines changed

lib/iris/fileformats/nimrod.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,33 @@
101101

102102

103103
# data specific header (int16) elements 108-159 (Fortran bytes 411-512)
104-
data_header_int16s = (
104+
table_1_data_header_int16s = (
105+
"radar_number"
106+
"radar_sites"
107+
"additional_radar_sites"
108+
"clutter_map_number"
109+
"calibration_type"
110+
"bright_band_height"
111+
"bright_band_intensity"
112+
"bright_band_test_param_1"
113+
"bright_band_test_param_2"
114+
"infill_flag"
115+
"stop_elevation"
116+
""
117+
"sensor_identifier"
118+
"meteosat_identifier"
119+
""
120+
"software_identifier"
121+
"software_major_version"
122+
"software_minor_version"
123+
"software_micro_version"
124+
""
125+
"period_seconds"
126+
)
127+
128+
129+
# data specific header (int16) elements 108-159 (Fortran bytes 411-512)
130+
table_2_data_header_int16s = (
105131
"threshold_type",
106132
"probability_method",
107133
"recursive_filter_iterations",
@@ -215,7 +241,8 @@ def _read_header(self, infile):
215241
self._read_header_subset(infile, general_header_float32s, np.float32)
216242
# skip unnamed floats
217243
infile.seek(4 * (28 - len(general_header_float32s)), os.SEEK_CUR)
218-
244+
threshold_set = True if self.threshold_value != -32767 else False
245+
print(threshold_set)
219246
# data specific header (float32) elements 60-104 (bytes 175-354)
220247
self._read_header_subset(infile, data_header_float32s, np.float32)
221248
# skip unnamed floats
@@ -226,6 +253,14 @@ def _read_header(self, infile):
226253
self.source = _read_chars(infile, 24)
227254
self.title = _read_chars(infile, 24)
228255

256+
# determine which of Table 1 or Table 2 is being used
257+
if threshold_set:
258+
table = "Table_2"
259+
data_header_int16s = table_2_data_header_int16s
260+
else:
261+
table = "Table_1"
262+
data_header_int16s = table_1_data_header_int16s
263+
229264
# data specific header (int16) elements 108- (bytes 411-512)
230265
self._read_header_subset(infile, data_header_int16s, np.int16)
231266
# skip unnamed int16s
@@ -239,6 +274,8 @@ def _read_header(self, infile):
239274
)
240275
)
241276

277+
return table
278+
242279
def _read_data(self, infile):
243280
"""Read the data array: int8, int16, int32 or float32.
244281
@@ -289,7 +326,7 @@ def _read_data(self, infile):
289326
self.data = self.data.reshape(self.num_rows, self.num_cols)
290327

291328

292-
def load_cubes(filenames, callback=None):
329+
def load_cubes(filenames, table, callback=None):
293330
"""Load cubes from a list of NIMROD filenames.
294331
295332
Parameters
@@ -317,7 +354,7 @@ def load_cubes(filenames, callback=None):
317354
# End of file. Move on to the next file.
318355
break
319356

320-
cube = iris.fileformats.nimrod_load_rules.run(field)
357+
cube = iris.fileformats.nimrod_load_rules.run(field, table)
321358

322359
# Were we given a callback?
323360
if callback is not None:

lib/iris/fileformats/nimrod_load_rules.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,38 @@ def add_attr(item):
660660
cube.attributes["institution"] = "Met Office"
661661

662662

663+
def table_1_attributes(cube, field):
664+
"""Add attributes to the cube."""
665+
666+
def add_attr(item):
667+
"""Add an attribute to the cube."""
668+
if hasattr(field, item):
669+
value = getattr(field, item)
670+
if is_missing(field, value):
671+
return
672+
if "radius" in item:
673+
value = f"{value} km"
674+
cube.attributes[item] = value
675+
676+
add_attr("radar_number")
677+
add_attr("radar_sites")
678+
add_attr("additional_radar_sites")
679+
add_attr("clutter_map_number")
680+
add_attr("calibration_type")
681+
add_attr("bright_band_height")
682+
add_attr("bright_band_intensity")
683+
add_attr("bright_band_test_param_1")
684+
add_attr("bright_band_test_param_2")
685+
add_attr("infill_flag")
686+
add_attr("stop_elevation")
687+
add_attr("sensor_identifier")
688+
add_attr("meteosat_identifier")
689+
add_attr("software_identifier")
690+
add_attr("software_major_version")
691+
add_attr("software_minor_version")
692+
add_attr("software_micro_version")
693+
694+
663695
def known_threshold_coord(field):
664696
"""Supply known threshold coord meta-data for known use cases.
665697
@@ -894,7 +926,7 @@ def time_averaging(cube, field):
894926
cube.attributes["processing"] = averaging_attributes
895927

896928

897-
def run(field, handle_metadata_errors=True):
929+
def run(field, table, handle_metadata_errors=True):
898930
"""Convert a NIMROD field to an Iris cube.
899931
900932
Parameters
@@ -930,10 +962,17 @@ def run(field, handle_metadata_errors=True):
930962
# vertical
931963
vertical_coord(cube, field)
932964

933-
# add other stuff, if present
934-
soil_type_coord(cube, field)
935-
probability_coord(cube, field, handle_metadata_errors)
936-
ensemble_member(cube, field)
965+
# add Table 1 specific stuff
966+
if table == "Table_1":
967+
table_1_attributes(cube, field)
968+
969+
# add Table 2 specific stuff
970+
if table == "Table_2":
971+
soil_type_coord(cube, field)
972+
probability_coord(cube, field, handle_metadata_errors)
973+
ensemble_member(cube, field)
974+
975+
# add other generic stuff, if present
937976
time_averaging(cube, field)
938977
attributes(cube, field)
939978

0 commit comments

Comments
 (0)