Skip to content

Commit f0fa676

Browse files
authored
Update um_fields_subset.py
added the funcationality to void validation
1 parent 03472d6 commit f0fa676

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

src/um_fields_subset.py

+34-21
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,46 @@ def validate_arguments(vlist, xlist, prognostic):
2727
if prognostic and (vlist or xlist):
2828
raise Exception("Error: -p incompatible with explicit list of variables")
2929

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+
3037
def parse_arguments():
3138

3239
parser = argparse.ArgumentParser(description="Subset UM fields based on user-specified options.")
3340

3441
# 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,
3845
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',
4047
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',
4249
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,
4451
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,
4653
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.')
4856
# Parse arguments
4957
args = parser.parse_args()
5058

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+
5463

5564
# Check if neither -v nor -x is provided
56-
if not vlist and not xlist:
65+
if not args.vlist and not args.xlist:
5766
raise argparse.ArgumentError(None, "Error: Either -v or -x must be specified.")
5867

5968
# Return arguments
60-
return args.input, args.output, args.nfields, args.prognostic, args.section, vlist, xlist
69+
return args
6170

6271
def match(code,vlist,section):
6372
if section:
@@ -117,7 +126,6 @@ def copy_fields(input_file, output_file, nfields, prognostic, vlist, xlist, sect
117126
def finalize_header(output_file, kout, nprog, ntracer):
118127
"""Finalize the output file header."""
119128

120-
121129
# Create a copy of the current integer constants
122130
integer_constants = output_file.integer_constants.copy()
123131

@@ -143,22 +151,27 @@ def finalize_header(output_file, kout, nprog, ntracer):
143151
output_file.integer_constants = integer_constants
144152

145153
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
148160

149161
#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)
151163

152164
#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)
154166

155167
# 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)
157169

158170
#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)
161173

162174
if __name__== "__main__":
163175
main()
164176

177+

0 commit comments

Comments
 (0)