forked from twdb/pyselfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfe2tec2D.py
executable file
·150 lines (122 loc) · 4.71 KB
/
selfe2tec2D.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
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
#!/usr/bin/python
#####################################################
## An python program to extract a level from the selfe
## binary and generate tecplot output files
##
## selfe2tec2D.py <datadir> <tecfile> <param> <nlevel> <nfiles>
##
## param = elev.61,hvel.63 etc
#####################################################
## Author : Dharhas Pothina
## Last Modified : 20080611
## Requires :
## No Other Scripts Required
#####################################################
programname = "selfe2tec2D.py";
# load required modules
import os
import struct
import sys
sys.path.append('/home/snegusse/test')
from string import strip
from numpy import *
from scipy import *
import pandas
import pyselfe
if len(sys.argv)==1 or sys.argv[1]=='help' or sys.argv[1]=='-help' or sys.argv[1
]=='--help' :
location=runBash("which " + programname)
message = runBash("awk \'/^##/ {print $0}\' < " + location);
sys.exit(message)
#read in command line variables
datadir=sys.argv[1]
tecfile=sys.argv[2]
param=sys.argv[3]
nlevel=sys.argv[4]
nfile=int(sys.argv[5])
model = pyselfe.Dataset(datadir + '/1_'+param)
# level = nlevel - 1 because of zero indexing
try:
nlevel = int(nlevel)
[t,t_iter,eta,dp,data] = model.read_time_series(param,levels=nlevel-1,
nfiles=nfile,
sfile=1,
datadir=datadir)
except ValueError:
if nlevel == 'all':
[t,t_iter,eta,dp,data] = model.read_time_series(param,
nfiles=nfile,
sfile=1,
datadir=datadir)
data = data[:,:,1:,:].mean(axis=2)
header = []
header.append('TITLE = Selfe : ' + strip(model.version))
variables = {'elev.61': '\"eta\"',
'hvel.64': '\"U\", \"V\"',
'salt.63': '\"Sal\"',
'temp.63': '\"T\"',
'vert.63': '\"W\"',
'prcp.61': '\"Precip\"',
'evap.61': '\"Evap\"',
'wind.62': '\"WX\", \"WY\"'}[strip(model.var_type)]
header.append('Variables = \"X\", \"Y\", \"ETA\", \"DP\", ' + variables)
zonetype = 'NODES=' + model.np.__int__().__str__() \
+ ', ELEMENTS=' + model.ne.__int__().__str__() \
+ ', DATAPACKING=BLOCK, ZONETYPE=FETRIANGLE'
header.append('ZONE T=\"0.0\"' + zonetype + ', SOLUTIONTIME=0.0')
fid = open(tecfile,'w')
for txt in header:
fid.write(txt + '\n')
#split into rows of 10
rows = model.np/10
remain = model.np-10*rows
end = model.np
savetxt(fid,model.x[0:10*rows].reshape(rows,10),delimiter=' ',fmt="%f")
model.x[10*rows:end].tofile(fid,sep=" ",format="%f")
fid.write('\n')
savetxt(fid,model.y[0:10*rows].reshape(rows,10),delimiter=' ',fmt="%f")
#fid.write('\n')
model.y[10*rows:end].tofile(fid,sep=" ",format="%f")
fid.write('\n')
dummy=zeros(model.np)
# at t=0 eta=0
savetxt(fid,dummy[0:10*rows].reshape(rows,10),delimiter=' ',fmt="%f")
#fid.write('\n')
dummy[10*rows:end].tofile(fid,sep=" ",format="%f")
fid.write('\n')
#change this to dp+eta later? Since dp is a constant
savetxt(fid,model.dp[0:10*rows].reshape(rows,10),delimiter=' ',fmt="%f")
#fid.write('\n')
model.dp[10*rows:end].tofile(fid,sep=" ",format="%f")
fid.write('\n')
#set all other vars =0 at t=0
for i in range(model.flag_sv):
savetxt(fid,dummy[0:10*rows].reshape(rows,10),delimiter=' ',fmt="%f")
#fid.write('\n')
dummy[10*rows:end].tofile(fid,sep=" ",format="%f")
fid.write('\n')
fid.write('\n')
# Save elem connectivity list
savetxt(fid,model.elem.reshape(model.elem.size/3,3),delimiter=' ',fmt="%d")
fid.write('\n')
for i in range(t.size):
tday = t[i]/86400.00
zone = 'ZONE T=\"' + tday.__str__() + '\", ' + zonetype + ',VARSHARELIST=([1,2,4]=1), CONNECTIVITYSHAREZONE=1, SOLUTIONTIME= ' +tday.__str__() +'\n'
fid.write(zone)
savetxt(fid,eta[i,:][0:10*rows].reshape(rows,10),delimiter=' ',fmt="%f")
eta[i,:][10*rows:end].tofile(fid,sep=" ",format="%f")
fid.write('\n')
# dp = eta[i,:] + model.dp
# savetxt(fid,dp[i,:][0:10*rows].reshape(rows,10),delimiter=' ',fmt="%f")
# dp[i,:][10*rows:end].tofile(fid,sep=" ",format="%f")
# fid.write('\n')
for j in range(model.flag_sv):
if (data.ndim==4):
data = data.reshape(data.shape[0],data.shape[1],data.shape[3])
tmpdata=data[i,:,j]
# print tmpdata.shape
# print tmpdata[0:10*rows].shape
savetxt(fid,tmpdata[0:10*rows].reshape(rows,10),delimiter=' ',fmt="%f")
tmpdata[10*rows:end].tofile(fid,sep=" ",format="%f")
fid.write('\n')
fid.close()