-
Notifications
You must be signed in to change notification settings - Fork 9
/
grd2vtk_cart
executable file
·121 lines (113 loc) · 3.37 KB
/
grd2vtk_cart
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
#!/bin/bash
#
# if 3D is set:
# given a set of equal geometry grd files describing a 3D scalar in
# cartesian, create a rectilinear vtk file (not projected, grd2vtk projects)
#
# else, just one layer
#
#
name=${1-tmp} # tmp.1.grd tmp.2.grd ....
dfile=${2-tmp.depths} # z_1 < z_2 < ...
is_3d=${3-1} # 0: one layer at zlevel
z_level=${4-1} # project layer to that depth
ascii=${5-0}
extrude=${6-0} # make exagerrated (by factor extrude) extrusion topography surface of a single layer
if [ $is_3d -eq 1 ];then
master_file=$name.1
if [ ! -s $dfile ];then
echo $0: $dfile for depths not found
exit
fi
nz=`lc $dfile`
if [ `echo $extrude | gawk '{if($1!=0)print(1);else print(0)}'` -ne 0 ];then
echo $0: error, extrude $extrude only works for single layer
exit
fi
else
master_file=$name
nz=1
fi
if [ ! -s $master_file.grd ];then
echo $0: $master_file.grd not found
exit
fi
nx=`grd2nx $master_file.grd`
ny=`grd2ny $master_file.grd`
reg=`grd2reg $master_file.grd`
inc=`grd2inc $master_file.grd`
((n=nx*ny*nz))
echo "# vtk DataFile Version 2.0"
echo converted from $name $dfile
if [ $ascii -eq 1 ];then
echo ASCII
else
echo BINARY
fi
if [ `echo $extrude | gawk '{if($1!=0)print(1);else print(0)}'` -eq 0 ];then
echo DATASET RECTILINEAR_GRID
echo DIMENSIONS $nx $ny $nz
echo X_COORDINATES $nx float
if [ $ascii -eq 1 ];then
grd2xyz $master_file.grd | gawk '{print($1)}' | sort -g | \
uniq | gawk '{printf("%.7e ",$1)}END{printf("\n")}'
else
grd2xyz $master_file.grd | gawk '{print($1)}' | sort -g | \
uniq | gawk '{print($1)}' | asciifloat2bebin 2> /dev/null
fi
echo Y_COORDINATES $ny float
if [ $ascii -eq 1 ];then
grd2xyz $master_file.grd | gawk '{print($2)}' | sort -g | \
uniq | gawk '{printf("%.7e ",$1)}END{printf("\n")}'
else
grd2xyz $master_file.grd | gawk '{print($2)}' | sort -g | \
uniq | gawk '{print($1)}' | asciifloat2bebin 2> /dev/null
fi
echo Z_COORDINATES $nz float
if [ $is_3d -eq 1 ];then
if [ $ascii -eq 1 ];then
gawk '{print($1)}' $dfile | gawk '{printf("%.7e ",$1)}END{printf("\n")}'
else
gawk '{print($1)}' $dfile | gawk '{print($1)}' | asciifloat2bebin 2> /dev/null
fi
else
if [ $ascii -eq 1 ];then
echo $z_level | gawk '{printf("%.7e ",$1)}END{printf("\n")}'
else
echo $z_level | gawk '{print($1)}' | asciifloat2bebin 2> /dev/null
fi
fi
echo POINT_DATA $n
echo SCALARS scalar float 1
echo LOOKUP_TABLE default
i=1
while [ $i -le $nz ];do
if [ $is_3d -eq 1 ];then
file=$name.$i.grd
else
file=$master_file.grd
fi
if [ $ascii -eq 1 ];then
grd2xyz -ZBLa $file | gawk '{printf("%.7e ",$1);n++;if(n==20){printf("\n");n=0;}}END{printf("\n")}'
else
grd2xyz -ZBLa $file | gawk '{print($1)}' | asciifloat2bebin 2> /dev/null
fi
((i=i+1))
done
else # extruded single layer
echo DATASET STRUCTURED_GRID
echo DIMENSIONS $nx $ny $nz
echo POINTS $n float
mean=`grd2mean $master_file.grd`
grdmath $master_file.grd $mean SUB = tmp.$$.grd
if [ $ascii -eq 1 ];then
grd2xyz tmp.$$.grd | \
gawk -v zl=$z_level -v scale=$extrude '{print($1,$2,zl+$3*scale)}' | \
gawk '{printf("%.7e %.7e %.7e\n",$1,$2,$3)}END{}'
else
grd2xyz tmp.$$.grd | \
gawk -v zl=$z_level -v scale=$extrude '{print($1,$2,zl+$3*scale)}' | \
asciifloat2bebin 2> /dev/null
fi
rm tmp.$$.grd
fi