-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmkwavescen_spe.py
executable file
·111 lines (95 loc) · 3.71 KB
/
mkwavescen_spe.py
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
#!/usr/bin/env python3
#
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
# #
# mkwavescen_spe.py #
# #
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
#
# Author: Pat Prodanovic, Ph.D., P.Eng.
#
# Date: Aug 2, 2022
#
# Purpose: This script creates the scenarios for the wave transformation
# library. Its inputs are the TOMAWAC base *.cas file, and the combinations
# of scenarios. The script will then write an individual *.cas file for each
# combination, as well as the bash script file to excecute the scenarios.
#
# This is same as mkwavescen.py, except the scenarios are made by using
# wave spectra files generated by a global wave model. This is applicable
# when a global model generates wave conditions at the outer boundary of
# the local model. The local model uses the spectra at its outer nodes
# as boundary conditions (as nesting).
#
# Uses: Python 2 or 3, Numpy
#
# Example:
#
# python mkwavescen_wind_spe.py -i base.cas -s scenarios.csv
# where:
# -i input TOMAWAC steering file (*.cas) for the base case scenario
# -s scenarios to include in the generation of library *.cas files; it must
# have first row with the following columns:
# id,wl,spe
# the scenario file must be comma delimited; id column is integer, wl is real,
# and the spe is a name of the spectral file that the local model will be
# forced with. There should be no header in the scenarios file!
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Global Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os,sys
import numpy as np
#
# I/O
if len(sys.argv) == 5:
base_case = sys.argv[2]
scenarios_file = sys.argv[4]
else:
print('Wrong number of arguments ... stopping now ...')
print('Usage:')
print('python mkwavescen_spe.py -i base.cas -s scenarios.csv')
sys.exit()
# the syntax here is for genfromtxt, which is different than loadtxt
# sneaky numpy ....
# to open the file that has all of the scenarios
scenarios = np.genfromtxt(scenarios_file, delimiter=',', dtype=np.str)
# scenario ids as integers
scen_id = scenarios[:,0].astype(int)
wl = scenarios[:,1].astype(float)
spe = scenarios[:,2]
n_scen = len(scen_id)
# to create a list of files
filenames = list()
for i in range(n_scen):
filenames.append("{:0>5d}".format(scen_id[i]) + ".cas")
# open the base case file, store contents of the base case file in a list
# where each line is a record
master = list();
with open(base_case, "r") as fp:
for line in fp:
master.append(line);
# to create a list of files
file_out = list()
# initialize the file counter
x = 0
for item in filenames:
file_out.append(item)
file_out[x] = open(item,'w')
for i in range(len(master)):
if (str(master[i]).find("2D RESULTS FILE = 'tom_erie_local.res'") == 0):
file_out[x].write('2D RESULTS FILE = ' + str("{:0>5d}".format(scen_id[x])) + '.slf' + '\n')
elif (str(master[i]).find("IMPOSED SPECTRA FILE = 'spectra_for_tom_local.spe'") == 0):
file_out[x].write('IMPOSED SPECTRA FILE = ' + spe[x] + '\n')
elif (str(master[i]).find("INITIAL STILL WATER LEVEL =") == 0):
file_out[x].write('INITIAL STILL WATER LEVEL = ' + str(wl[x]) + '\n')
else:
file_out[x].write(str(master[i]))
file_out[x].close()
x = x + 1
# now to create a bash script file that will run all the scenarios
fs = open("run_scenarios.sh", "w")
for item in filenames:
fs.write("runcode.py tomawac -s --ncsize=6 " + item + "\n")
fs.write("rm *.sortie" + "\n")
fs.write("rm *.zip" + "\n")