-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_pism
executable file
·299 lines (224 loc) · 10.5 KB
/
run_pism
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
# -*- mode:python -*-
import argparse
import json
import os
import shlex
import shutil
import subprocess
import sys
import flo_utils as fu
def backup_file(filename):
if os.access(filename, os.R_OK):
print ("file " + filename + " exists. Moving it to " + filename + "~")
shutil.move(filename, filename + "~")
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="verbose output",
action="store_true", default = False)
parser.add_argument("-e", "--echo", help="only print command",
action="store_true", default = False)
parser.add_argument("-i", "--input", help="input file", default = None, required = True)
parser.add_argument("-c", "--config", help="run_pism config file", default="run_pism.conf")
parser.add_argument("-a", "--atmosphere_given_file", help="atmosphere input")
parser.add_argument("--surface_given_file", help="smb+tsurf input")
parser.add_argument("--topg_file", help="overwrite topg")
parser.add_argument("--ocean_th_file", help="ocean forcing input")
parser.add_argument("--ocean_delta_SL_file", help="varying sea level input")
parser.add_argument("-o", "--output", help="output file", default = None, required=True)
parser.add_argument("--ys", help="startyear", default = None)
parser.add_argument("-b", "--bootstrap", help="bootstrap (instead of restart)" , action="store_true" )
parser.add_argument("-r", "--regrid_file", help="regrid file (if desired)" , default = None )
parser.add_argument("--config_override", help="config_override file (if desired)" , default = None )
parser.add_argument("-p" ,"--pass_arguments", help="extra arguments to be passed on, quote and start with a space" , default = None )
parser.add_argument("--force_to_thk", help="surface forcing file" , default = None )
parser.add_argument("--force_to_thk_alpha", help="surface forcing alpha" , default = None )
parser.add_argument("--smb_only", help="only compute SMB, no flow fields" , action = "store_true" , default=False)
group = parser.add_mutually_exclusive_group()
group.add_argument("-y", "--years", help="years to run", default = None)
group.add_argument("--time_file", help="time file to specify run duration", default = None)
group = parser.add_mutually_exclusive_group()
group.add_argument("--ssa", action="store_true", help="use ssa")
group.add_argument("--no-ssa", action="store_false", dest = "ssa", help="do not use ssa")
group = parser.add_mutually_exclusive_group()
group.add_argument("--no_mpi", action="store_true", help="run without mpi", default = False)
group.add_argument("--mpi", action="store_false", dest = "no_mpi", help="run with mpi (default)")
args = parser.parse_args()
if args.verbose:
fu.debug = True
args_dict = vars(args)
pism_args = []
config = {}
if os.access(args.config, os.R_OK):
config = json.load(open(args.config,"r"))
else:
fu.cerr("RUN_PISM: COULD NOT FIND CONFIG FILE %s"%args.config)
sys.exit(666)
for x in args_dict.keys():
if not args_dict[x] is None :
config[x] = args_dict[x]
# fu.debug_cerr([(x,config[x]) for x in config.keys()])
errors = 0
if not "years" in config.keys() and not "time_file" in config.keys():
print ("run_pism:ERROR: Don't know for how long to run. Need -y or --time_file! GOING TO EXIT")
errors = errors + 1
if args.input:
pism_args.append(" -i " + args.input)
if args.output[-3:] == ".nc":
output_start = args.output[:-3]
dn = os.path.dirname(output_start)
if dn:
fu.mkdir(dn)
else:
fu.mkdir(output_start)
output_start="%s/%s"%(output_start,output_start)
else:
print ("run_pism: ERROR: need netcdf output file name ending on '.nc' ! GOING TO EXIT!")
errors = errors + 1
if errors:
print ("arguments:")
print (args_dict)
print ("full config:")
print (config)
sys.exit(1)
if args.smb_only:
config["ssa"] = False
pism_args.append("-test_climate_models -no_mass")
if config["ssa"]:
# pism_args.append("-ssa_sliding")
pism_args.append("-stress_balance ssa+sia")
if args.bootstrap:
boot=" -bootstrap " + config["geometry"] + config["bootopts"]
pism_args.append(boot)
if args.regrid_file:
regrid=" -regrid_vars litho_temp,thk,enthalpy,tillwat,tillphi,bmelt -regrid_file %s "%(args.regrid_file)
pism_args.append(regrid)
if "years" in config.keys():
pism_args.append(" -y " + config["years"])
if "ys" in config.keys():
pism_args.append("-ys " + config["ys"])
elif "time_file" in config.keys():
pism_args.append(" -time_file %s "%(config["time_file"]))
else:
fu.cerr("GOT NEITHER years nor time_file. Should not have got this far in the code. WHATEVER I'll call it a day...'")
sys.exit(666)
pism_args.append("-o " + output_start+".nc")
if "max_dt" in config.keys():
pism_args.append(" -max_dt " + config["max_dt"])
if ("atmosphere_given_file" in config.keys()):
atmosphere = " -atmosphere given,lapse_rate " + \
" -atmosphere_given_file " + config["atmosphere_given_file"]
if "temp_lapse_rate" in config.keys():
atmosphere += " -temp_lapse_rate " + config["temp_lapse_rate"]
if "atmosphere_lapse_rate_file" in config.keys():
atmosphere +=" -atmosphere_lapse_rate_file " + config["atmosphere_lapse_rate_file"]
else:
atmosphere +=" -atmosphere_lapse_rate_file " + config["atmosphere_given_file"]
pism_args.append(atmosphere)
elif ("atmosphere_given_file" in config.keys()):
atmosphere = " -atmosphere given " + " -atmosphere_given_file " + config["atmosphere_given_file"]
pism_args.append(atmosphere)
if "atmosphere_given_period" in config.keys() :
pism_args.append("-atmosphere_given_period " + config["atmosphere_given_period"])
if "surface" in config.keys():
surface=config["surface"]
if "force_to_thk" in config.keys():
surface = surface + ",forcing"
pism_args.append(" -force_to_thk " + config["force_to_thk"])
if "force_to_thk_alpha" in config.keys():
pism_args.append("-force_to_thk_alpha " + config["force_to_thk_alpha"] )
if "surface_cache_update_interval" in config.keys():
surface = surface + ",cache"
pism_args.append("-surface_cache_update_interval " + config["surface_cache_update_interval"])
if config["surface"] == "given" and config.get("surface_given_file",False):
pism_args.append(" -surface_given_file %s "%config["surface_given_file"])
pism_args.append("-surface "+ surface)
if "ocean_kill_file" in config.keys():
pism_args.append(" -calving ocean_kill -ocean_kill_file " + config["ocean_kill_file"])
ocean=""
if "ocean_th_file" in config.keys():
pism_args.append(" -ocean_th_file " + config["ocean_th_file"])
ocean = "th"
if "ocean_th_period" in config.keys():
pism_args.append("-ocean_th_period " + config["ocean_th_period"])
if not ocean:
ocean ="th"
else:
if not "th" in ocean:
ocean+=",th"
if "ocean_delta_SL_file" in config.keys():
pism_args.append("-ocean_delta_SL_file " + config["ocean_delta_SL_file"])
if not ocean:
ocean ="delta_SL"
else:
ocean+=",delta_SL"
if ocean:
pism_args.append(" -ocean %s "%ocean)
if "topg_file" in config.keys():
pism_args.append("-topg_file")
pism_args.append(config["topg_file"])
if "config_override" in config.keys():
pism_args.append("-config_override " + config["config_override"])
config_override=(config["config_override"])
cdl_file = config_override[:-3] + ".cdl"
if os.access(cdl_file, os.R_OK):
if os.stat(cdl_file).st_mtime > os.stat(config_override).st_mtime:
fu.cerr("\n\nERROR: CONFIG HAS A NEWER CDL FILE! REFUSING TO USE NETCDF")
fu.cerr("run")
fu.cerr("ncgen -o %s %s"%(config_override, cdl_file))
fu.cerr("or")
fu.cerr("touch %s"%(config_override))
fu.cerr("to fix")
sys.exit(333)
# test if there is a newer cdl. Die and issue warning, if so.
if ("bed_smoother_range" in config.keys()):
pism_args.append("-bed_smoother_range " + config["bed_smoother_range"])
if ("extra_times" in config.keys()):
pism_args.append("-extra_times " + config["extra_times"])
pism_args.append("-extra_vars " + config["extra_vars"])
pism_args.append("-extra_file " + output_start + "_extra")
pism_args.append("-extra_split")
if ("ts_times" in config.keys()):
pism_args.append(" -ts_times " + config["ts_times"])
pism_args.append("-ts_file " + output_start + "_ts.nc")
if ("ts_vars" in config.keys()):
pism_args.append(" -ts_vars " + config["ts_vars"])
if "direct" in config.keys():
print (config["direct"])
if "pass_arguments" in config.keys(): # anything - coming from the command line
if "direct" in config.keys():
config["direct"].append(config["pass_arguments"])
else:
config["direct"] = [config["pass_arguments"]]
if ("direct" in config.keys()):
print (config["direct"])
pism_args.append(" ".join(config["direct"]))
# pism_args.append(config["skip"])
# if args.pass_arguments:
# pism_args.append(args.pass_arguments)
if args.no_mpi:
start = config["pism"]
else:
start = " ".join((config["mpi"], config["pism"]))
print (pism_args)
full_pism_call = start + " " + " ".join(pism_args)
config["full_pism_call"] = full_pism_call
config_file_name = output_start + ".conf"
backup_file(config_file_name)
json.dump(config, open(config_file_name, "w"), sort_keys=True, indent=2)
error_file = output_start + ".err"
backup_file(error_file)
log_file = output_start + ".log"
backup_file(log_file)
print (" running " + full_pism_call)
print ("tail -f ", log_file , error_file)
spc = subprocess.check_call(shlex.split(full_pism_call), stdout = open(log_file, "w"), stderr= open(error_file, "w"))
if not os.access(output_start+".nc", os.R_OK):
fu.cerr("Can't find output file " + output_start+".nc\n")
sys.exit(1)
if os.path.getctime(output_start+".nc") < os.path.getctime(config_file_name):
fu.cerr("RUN_PISM: Output file %s is older than config file %s. Something went wrong!\n"%(output_start+".nc", config_file_name))
sys.exit(1)
if "years" in config.keys():
fu.qo(("ncap2", "-Ovs", "liquid_flux=(climatic_mass_balance_cumulative+grounded_basal_flux_cumulative+nonneg_flux_cumulative)/%s;solid_flux=(floating_basal_flux_cumulative+discharge_flux_cumulative)/%s"%(config["years"],config["years"]), output_start+".nc", output_start+"_fluxes.nc"))
fu.qo(("ncatted", "-a", "units,liquid_flux,o,c,kg m-2 yr-1", "-a", "units,solid_flux,o,c,kg m-2 yr-1", output_start+"_fluxes.nc"))
# spc.communicate()