Skip to content

Commit 07b1b3a

Browse files
Update src/perturbIC.py
Co-authored-by: Davide Marchegiani <[email protected]>
1 parent 13f5bc6 commit 07b1b3a

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/perturbIC.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,30 @@ def remove_timeseries(ff):
8787
return ff
8888

8989

90-
def create_outfile(args):
90+
def create_default_outname(filename, suffix="_perturbed"):
9191
"""
92-
This provides an outline for editing if a new file should be
93-
created
92+
Create a default output filename by appending a suffix to the input filename.
93+
If an output filename already exists, a number will be appended to produce a unique output filename.
9494
9595
Parameters
9696
----------
97-
args: ArgumentParser object
98-
The argument parser object with output file name
97+
filename: str
98+
The input filename.
99+
suffix: str, optional
100+
The suffix to append to the filename.
99101
100102
Returns
101103
----------
102-
output_file - str - This is a string of an output name
104+
output_filename: str
105+
The default output filename.
103106
"""
104-
105-
#Seperate the string into the extension and the base
106-
basename, ext = os.path.splitext(args.ifile)
107-
output_filename = basename + '_perturbed' + ext
108-
109-
#Check if that name alreay exists
107+
output_filename = f"{filename}{suffix}"
108+
num=""
110109
if os.path.exists(output_filename):
111-
raise FileExistsError(f"The file '{output_filename}' already exists. Cannot save over the file")
112-
else:
113-
return output_filename
110+
num = 1
111+
while os.path.exists(f"{output_filename}{num}"):
112+
num += 1
113+
return f"{output_filename}{num}"
114114

115115

116116
def create_perturbation(args, rs, nlon, nlat):

0 commit comments

Comments
 (0)