forked from jasonfleming/pputils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adcirc2png.py
executable file
·167 lines (141 loc) · 4.73 KB
/
adcirc2png.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
#
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
# #
# adcirc2png.py #
# #
#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!
#
# Author: Pat Prodanovic, Ph.D., P.Eng.
#
# Date: Oct 2, 2017
#
# Purpose: Script takes in a mesh in ADCIRC format, and outputs a *.png
# file using Matplotlib for quick visualization. Additional parameters
# that control the levels and zoom are given in the file adcirc2png.cfg.
#
# Revised: Dec 5, 2017
# Made the *.cfg file a required command line input.
#
# Revised: Mar 8, 2018
# If the colour code is set to none, then the script will write only
# the mesh to the *.png file. This is to be used for quick visualization
# of the mesh, without the need to open a mesh viewer.
#
# Uses: Python 2 or 3, Numpy
#
# Example:
#
# python adcirc2png.py -i out.grd -o out.png
# where:
# -i input adcirc mesh file
# -o output *.vtk file
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Global Imports
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import os,sys
import numpy as np
import matplotlib.tri as mtri
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from ppmodules.readMesh import *
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# MAIN
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
curdir = os.getcwd()
#
# I/O
if len(sys.argv) != 7 :
print('Wrong number of Arguments, stopping now...')
print('Usage:')
print('python adcirc2png.py -i out.grd -c adcirc2png.cfg -o out.png')
sys.exit()
adcirc_file = sys.argv[2]
cfg_file = sys.argv[4]
png_file = sys.argv[6]
# reads the sel2png.cfg file for additional parameters
# each line in the file is a list object
line = list()
with open(cfg_file, 'r') as f1:
for i in f1:
line.append(i)
# reads the first set of parameters from the *.cfg file
params1 = line[1].split()
cbar_min_global = float(params1[0])
cbar_max_global = float(params1[1])
cbar_color_map = params1[2]
# reads the second set of parameters from the *.cfg file
params2 = line[13].split()
zoom = int(params2[0])
xll = float(params2[1])
yll = float(params2[2])
xur = float(params2[3])
yur = float(params2[4])
# reads the third set of parameters from the *.cfg file
params3 = line[23].split()
show_mesh = int(params3[0])
lineweight = float(params3[1])
mesh_color = params3[2]
# read the adcirc file
n,e,x,y,z,ikle = readAdcirc(adcirc_file)
# creates a triangulation grid using matplotlib function Triangulation
triang = mtri.Triangulation(x, y, ikle)
if ((cbar_min_global == -1) and (cbar_max_global == -1)):
# this is the range of the colour coding
cbar_min = np.min(z)
cbar_max = np.max(z)
if ((cbar_max - cbar_min) < 0.001):
cbar_max = cbar_min + 0.001
else:
cbar_min = cbar_min_global
cbar_max = cbar_max_global
# the flag that tells if the limits are default or not
is_default = (cbar_min_global == -1) and (cbar_max_global == -1)
# adjust the plot_array for limits of levels (before plotting)
# added on 2018.07.28
if (cbar_max_global > 0 and is_default == False):
#print('I am in the +ve section')
for i in range(len(z)):
if (z[i] <= cbar_min):
z[i] = cbar_min + cbar_min*0.01
if (z[i] >= cbar_max):
z[i] = cbar_max - cbar_max*0.01
if (cbar_min_global < 0 and cbar_max_global < 0 and is_default == False):
#print('I am in the -ve section')
for i in range(len(z)):
if (z[i] <= cbar_min):
z[i] = cbar_min - cbar_min*0.01
if (z[i] >= cbar_max):
z[i] = cbar_max + cbar_max*0.01
# adjust the levels
levels = np.linspace(cbar_min, cbar_max, 16)
plt.figure()
plt.gca().set_aspect('equal')
if (cbar_color_map != 'none'):
cmap = cm.get_cmap(name=cbar_color_map)
# this plots the triangulation only
if (show_mesh > 0):
plt.triplot(triang, lw=lineweight, color=mesh_color)
if (cbar_color_map != 'none'):
# this plots the colour coded z values
plt.tricontourf(triang, z, levels=levels, cmap=cmap, antialiased=True)
# this is for the colorbar
cb = plt.colorbar(orientation='vertical', shrink=0.3,format='%.3f')
cb.set_ticks(levels)
cb.ax.tick_params(labelsize=5)
# determine the axis label
title = 'z [m]'
# set the title, and its size
cb.ax.set_title(title, size=5)
# axis limits (the zoom flag controls this)
if (zoom > 0):
plt.xlim(xll,xur)
plt.ylim(yll,yur)
plt.axis('off')
# this plots the figure
fig = plt.gcf() # returns the reference for the current figure
fig.set_size_inches(12,9)
fig.savefig(png_file, dpi=300, bbox_inches='tight', transparent=False)
plt.close()