forked from jasonfleming/pputils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
executable file
·111 lines (98 loc) · 2.97 KB
/
extract.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
#!/usr/bin/env python3
#
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
# #
# extract.py #
# #
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
#
# Author: Pat Prodanovic, Ph.D., P.Eng.
#
# Date: Feb 15, 2016
#
# Purpose: Script designed to open 2D telemac binary file and write the
# values of all variables to a text file for the selected time step.
#
# Revised: Feb 18, 2017
# Got rid of the limitation that only one or two variables could be read.
#
# Revised: Mar 9, 2017
# Deleted the trailing comma in the text output.
#
# Uses: Python 2 or 3, Numpy
#
# Usage:
# python extract.py -i result.slf -t 8 -o result.csv
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Global Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import sys
import numpy as np
from ppmodules.selafin_io_pp import *
#
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# MAIN
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# I/O
if len(sys.argv) == 7:
input_file = sys.argv[2] # input *.slf file
t = int(sys.argv[4]) # index of time record
output_file = sys.argv[6] # output text file
else:
print('Wrong number of arguments ... stopping now ...')
print('Usage:')
print('python extract.py -i result.slf -t 8 -o result.csv')
sys.exit()
# create the output file
fout = open(output_file, 'w')
#
# Read the header of the selafin result file and get geometry and
# variable names and units
# use selafin_io_pp class ppSELAFIN
slf = ppSELAFIN(input_file)
slf.readHeader()
slf.readTimes()
NPLAN = slf.getNPLAN()
if (NPLAN > 1):
print('It does not make sense to run this script for 3d SELAFIN files.')
print('Exiting!!!')
sys.exit(0)
times = slf.getTimes()
vnames = slf.getVarNames()
vunits = slf.getVarUnits()
x = slf.getMeshX()
y = slf.getMeshY()
# number of variables
NVAR = len(vnames)
# now we are ready to output the results
# to write the header of the output file
fout.write('x, ' + 'y, ')
for i in range(NVAR):
if (i < (NVAR-1)):
fout.write(vnames[i] + ', ')
else:
fout.write(vnames[i])
fout.write('\n')
# do not display units
'''
fout.write('M, M, ')
for i in range(NVAR):
fout.write(vunits[i] + ', ')
fout.write('\n')
'''
# read the variables for the specified time step t
slf.readVariables(t)
results = slf.getVarValues()
# use numpy to transpose the results
results_tr = np.transpose(results)
# outputs the results
for k in range(len(x)):
fout.write(str('{:.12f}').format(x[k]) + ', ' + str('{:.12f}').format(y[k]) + ', ')
for j in range(NVAR):
if (j < (NVAR-1)):
fout.write(str("{:.12f}").format(results_tr[k][j]) + ', ')
else:
fout.write(str("{:.12f}").format(results_tr[k][j]))
fout.write('\n')