@@ -159,55 +159,44 @@ def expand_func(varname):
159159 packing = field_block ["packing" ]
160160 field_list_1d = get_all_fields (field_block )
161161
162- # seperate field_name and output_name:
162+ # seperate field_name, alias, and reduction method
163+ # (the latter two are optional)
163164 field_list_1d_seperated = []
164165 for field in field_list_1d :
165166 field_split = field .split (":" )
166- if len (field_split ) == 2 :
167- field = field_split [0 ], field_split [1 ]
168- elif len (field_split ) == 1 :
169- field = field_split [0 ], field_split [0 ]
170- else :
171- raise RuntimeError (
172- "Cannot infer field name and output name for " + field
173- )
174- field_list_1d_seperated .append (field )
167+ field_name = field_split [0 ]
168+ alias = field_name
169+ reduction = file_block ["reduction_method" ]
170+ assert 1 <= len (field_split ) <= 3 , (
171+ "Invalid field format: " + field
172+ )
173+ if len (field_split ) >= 2 :
174+ alias = field_split [1 ]
175+ if len (field_split ) >= 3 :
176+ reduction = field_split [2 ]
177+
178+ field_list_1d_seperated .append ((field_name , alias , reduction ))
175179
176180 # check if there are any duplicate fields in the same file:
177- for field_name , output_name in field_list_1d_seperated :
178- assert field_name not in all_fields , (
179- 'Field "'
180- + field_name
181- + '" is listed more than once'
182- + " in file: "
183- + file_block ["suffix" ]
184- )
185- all_fields .append (field_name )
181+ field_set = set ()
182+ for field_name , alias , reduction in field_list_1d_seperated :
183+ if alias in field_set :
184+ raise ValueError (
185+ 'Field "'
186+ + alias
187+ + '" is listed more than once'
188+ + " in file: "
189+ + file_block ["suffix" ]
190+ )
191+ field_set .add (alias )
186192
187193 mfnl = max ([len (field ) for field in field_list_1d ]) + 3
188194 mfnl = min (16 , mfnl ) # limit to 16
189- for field_name , output_name in field_list_1d_seperated :
195+ w = lambda s : f'"{ s } ",' # wrap string in quotes and add comma
196+ for field_name , alias , reduction in field_list_1d_seperated :
190197 diag_table .write (
191- (
192- "{module_name:14s} {field_name:"
193- + str (mfnl )
194- + "}{output_name:"
195- + str (mfnl )
196- + "}"
197- '{fname} "all", {reduction_method} {regional_section} {packing}\n '
198- ).format (
199- module_name = '"' + module + '",' ,
200- field_name = '"' + field_name + '",' ,
201- output_name = '"' + output_name + '",' ,
202- fname = fname + "," ,
203- reduction_method = '"'
204- + str (file_block ["reduction_method" ])
205- + '",' ,
206- regional_section = '"'
207- + str (file_block ["regional_section" ])
208- + '",' ,
209- packing = str (packing ),
210- )
198+ f'{ w (module )} { w (field_name ):{mfnl }} { w (alias ):{mfnl }} { fname } , "all", '
199+ f'{ w (reduction )} { w (file_block ["regional_section" ])} { packing } \n '
211200 )
212201
213202 diag_table .write ("\n " )
0 commit comments