@@ -27,37 +27,46 @@ def validate_arguments(vlist, xlist, prognostic):
27
27
if prognostic and (vlist or xlist ):
28
28
raise Exception ("Error: -p incompatible with explicit list of variables" )
29
29
30
+ def void_validation (* args , ** kwargs ):
31
+ """
32
+ Don't perform the validation, but print a message to inform that validation has been skipped.
33
+ """
34
+ print ('Skipping mule validation. To enable the validation, run using the "--validate" option.' )
35
+ return
36
+
30
37
def parse_arguments ():
31
38
32
39
parser = argparse .ArgumentParser (description = "Subset UM fields based on user-specified options." )
33
40
34
41
# Define arguments
35
- parser .add_argument ('-i' , '--input' , required = True , help = "Input file" )
36
- parser .add_argument ('-o' , '--output' , required = True , help = "Output file" )
37
- parser .add_argument ('-n' , '--nfields' , type = int , default = 9999999999 ,
42
+ parser .add_argument ('-i' , '--input' , dest = 'ifile' , required = True , help = "Input file" )
43
+ parser .add_argument ('-o' , '--output' , dest = 'ofile' , required = True , help = "Output file" )
44
+ parser .add_argument ('-n' , '--nfields' , dest = 'nfields' , type = int , default = 9999999999 ,
38
45
help = "Maximum number of fields to process (default: 9999999999)" )
39
- parser .add_argument ('-p' , '--prognostic' , action = 'store_true' ,
46
+ parser .add_argument ('-p' , '--prognostic' , dest = 'prognostic' , action = 'store_true' ,
40
47
help = "Include only prognostic (section 0,33,34) variables" )
41
- parser .add_argument ('-s' , '--section' , action = 'store_true' ,
48
+ parser .add_argument ('-s' , '--section' , dest = 'section' , action = 'store_true' ,
42
49
help = "Use section numbers instead of variable indices for -v and -x" )
43
- parser .add_argument ('-v' , '--vlist' , type = str ,
50
+ parser .add_argument ('-v' , '--vlist' , dest = 'vlist' , type = str ,
44
51
help = "Comma-separated list of variables to INCLUDE (STASH indices)" )
45
- parser .add_argument ('-x' , '--xlist' , type = str ,
52
+ parser .add_argument ('-x' , '--xlist' , dest = 'xlist' , type = str ,
46
53
help = "Comma-separated list of variables to EXCLUDE (STASH indices)" )
47
-
54
+ parser .add_argument ('--validate' , action = 'store_true' ,
55
+ help = 'Validate the output fields file using mule validation.' )
48
56
# Parse arguments
49
57
args = parser .parse_args ()
50
58
51
- # Process lists from strings to integers
52
- vlist = [int (v ) for v in args .vlist .split ("," )] if args .vlist else []
53
- xlist = [int (x ) for x in args .xlist .split ("," )] if args .xlist else []
59
+ #Convert from string to int
60
+ args .vlist = [int (v ) for v in args .vlist .split ("," )] if args .vlist else []
61
+ args .xlist = [int (x ) for x in args .xlist .split ("," )] if args .xlist else []
62
+
54
63
55
64
# Check if neither -v nor -x is provided
56
- if not vlist and not xlist :
65
+ if not args . vlist and not args . xlist :
57
66
raise argparse .ArgumentError (None , "Error: Either -v or -x must be specified." )
58
67
59
68
# Return arguments
60
- return args . input , args . output , args . nfields , args . prognostic , args . section , vlist , xlist
69
+ return args
61
70
62
71
def match (code ,vlist ,section ):
63
72
if section :
@@ -117,7 +126,6 @@ def copy_fields(input_file, output_file, nfields, prognostic, vlist, xlist, sect
117
126
def finalize_header (output_file , kout , nprog , ntracer ):
118
127
"""Finalize the output file header."""
119
128
120
-
121
129
# Create a copy of the current integer constants
122
130
integer_constants = output_file .integer_constants .copy ()
123
131
@@ -143,22 +151,27 @@ def finalize_header(output_file, kout, nprog, ntracer):
143
151
output_file .integer_constants = integer_constants
144
152
145
153
def main ():
146
- ifile , ofile , nfields , prognostic , section , vlist , xlist = parse_arguments ()
147
- validate_arguments (vlist , xlist , prognostic )
154
+ args = parse_arguments ()
155
+ validate_arguments (args .vlist , args .xlist , args .prognostic )
156
+
157
+ # Skip the mule validation if the "--validate" option is provided
158
+ if args .validate :
159
+ mule .DumpFile .validate = void_validation
148
160
149
161
#Create the output UM file that will be saved to
150
- f , g = initialize_output_file (ifile , ofile )
162
+ f , g = initialize_output_file (args . ifile , args . ofile )
151
163
152
164
#Find the fields, if any, that needs a land-sea mask
153
- check_packed_fields (f , vlist , prognostic , section )
165
+ check_packed_fields (f , args . vlist , args . prognostic , args . section )
154
166
155
167
# Loop over all the fields, counting the number of prognostic fields
156
- kout , nprog , ntracer = copy_fields (f , g , nfields , prognostic , vlist , xlist , section )
168
+ kout , nprog , ntracer = copy_fields (f , g , args . nfields , args . prognostic , args . vlist , args . xlist , args . section )
157
169
158
170
#Create the header and make sure it is large enough
159
- finalize_header (g , kout , nprog , ntracer )
160
- g .close ( )
171
+ # finalize_header(g, kout, nprog, ntracer)
172
+ g .to_file ( args . ofile )
161
173
162
174
if __name__ == "__main__" :
163
175
main ()
164
176
177
+
0 commit comments