5454 'typicalSecond'
5555]
5656
57+ HEADERS = ["edition" , "masterTableNumber" , "bufrHeaderCentre" ,
58+ "bufrHeaderSubCentre" , "updateSequenceNumber" , "dataCategory" ,
59+ "internationalDataSubCategory" , "dataSubCategory" ,
60+ "masterTablesVersionNumber" , "localTablesVersionNumber" ,
61+ "typicalYear" , "typicalMonth" , "typicalDay" , "typicalHour" ,
62+ "typicalMinute" , "typicalSecond" ,
63+ "numberOfSubsets" , "observedData" , "compressedData" ,
64+ "unexpandedDescriptors" ]
65+
5766
5867class ObservationDataBUFR (BaseAbstractData ):
59- """Oservation data"""
68+ """Observation data"""
6069
6170 def transform (
6271 self , input_data : Union [Path , bytes ], filename : str = ''
@@ -97,6 +106,13 @@ def transform_message(self, bufr_in: int) -> None:
97106 LOGGER .error (f'Error unpacking message: { err } ' )
98107 raise err
99108
109+ # get descriptors present in the file
110+ descriptors = codes_get_array (bufr_in , "expandedDescriptors" ).tolist ()
111+ # get the headers in hte file
112+ headers = {}
113+ for header in HEADERS :
114+ headers [header ] = codes_get (bufr_in , header )
115+
100116 num_subsets = codes_get (bufr_in , 'numberOfSubsets' )
101117 LOGGER .debug (f'Found { num_subsets } subsets' )
102118
@@ -112,15 +128,41 @@ def transform_message(self, bufr_in: int) -> None:
112128 idx = i + 1
113129 LOGGER .debug (f'Processing subset { idx } ' )
114130
115- LOGGER .debug ('Copying template BUFR' )
116- subset_out = codes_clone (TEMPLATE )
117- codes_set (subset_out , 'masterTablesVersionNumber' , table_version )
118- codes_set_array (subset_out , 'unexpandedDescriptors' , outUE )
119-
120131 LOGGER .debug ('Extracting subset' )
121132 codes_set (bufr_in , 'extractSubset' , idx )
122133 codes_set (bufr_in , 'doExtractSubsets' , 1 )
123134
135+ # copy the replication factors
136+ if 31000 in descriptors :
137+ short_replication_factors = codes_get_array (bufr_in , "shortDelayedDescriptorReplicationFactor" ).tolist () # noqa
138+ if 31001 in descriptors :
139+ replication_factors = codes_get_array (bufr_in , "delayedDescriptorReplicationFactor" ).tolist () # noqa
140+ if 31002 in descriptors :
141+ extended_replication_factors = codes_get_array (bufr_in , "extendedDelayedDescriptorReplicationFactor" ).tolist () # noqa
142+
143+ LOGGER .debug ('Copying template BUFR' )
144+ subset_out = codes_clone (TEMPLATE )
145+
146+ # set the replication factors, this needs to be done before
147+ # setting the unexpanded descriptors
148+ if 31000 in descriptors :
149+ codes_set_array (subset_out , "inputShortDelayedDescriptorReplicationFactor" , short_replication_factors ) # noqa
150+ if 31001 in descriptors :
151+ codes_set_array (subset_out , "inputDelayedDescriptorReplicationFactor" , replication_factors ) # noqa
152+ if 31002 in descriptors :
153+ codes_set_array (subset_out , "inputExtendedDelayedDescriptorReplicationFactor" , extended_replication_factors ) # noqa
154+
155+ # we need to copy all the headers, not just the
156+ # unexpandedDescriptors and MT number
157+ headers ['unexpandedDescriptors' ] = outUE
158+ headers ['masterTablesVersionNumber' ] = table_version
159+ headers ['numberOfSubsets' ] = 1
160+ for k , v in headers .items ():
161+ if isinstance (v , list ):
162+ codes_set_array (subset_out , k , v )
163+ else :
164+ codes_set (subset_out , k , v )
165+
124166 LOGGER .debug ('Cloning subset to new message' )
125167 subset = codes_clone (bufr_in )
126168 self .transform_subset (subset , subset_out )
0 commit comments