-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_spd.gmt.pl
345 lines (300 loc) · 10.7 KB
/
plot_spd.gmt.pl
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use Geo::Proj4;
my $args = @ARGV;
if ($args < 1) {
print qq(USAGE: perl $0 <yourfile.conf> <XYZ_spatial_density_file>\n\n);
exit;
}
open (LOG, ">>logfile") || die ("$!");
print STDERR "Logging run info to: logfile\n";
open (CONF, "<$ARGV[0]") || die ("$!");
my %P;
my $key;
my $value;
while (<CONF>) {
if ($_ =~ /^#/ || $_ =~ /^\n/) {}
else { ($key, $value) = split "=",$_;
chomp($value);
$P{$key} = $value;
print STDERR qq($key $value\n);
}
}
close CONF;
$in = $ARGV[1];
$events = $P{EVENT_FILE};
$aoi = $P{AOI_FILE};
$utm_west = $P{WEST};
$utm_east = $P{EAST};
$utm_south = $P{SOUTH};
$utm_north = $P{NORTH};
$utm_zone = $P{UTM_ZONE};
$grid_spacing = $P{GRID_SPACING};
$plot = $P{PLOT};
$map_scale = $P{MAP_SCALE};
$scale = qq(1:$map_scale).qq(m);
$res = 300;
$psscaleX = 12;
$psscaleY = 10;
if ($plot == 0) {
print STDERR qq(Done!\n);
exit(0);
}
$contours = qq(contours);
$cpt = qq(colors.cpt);
open(CONTOURS, ">$contours") || die("can't open $contours: $!");
open(CPT, ">$cpt") || die("cannot open $cpt: $!");
print LOG "\n Plotting spatial density contours .....\n";
$ct=0;
$data_sum = 0;
if ($plot == 1 or $plot == 3) {
print LOG qq(Opening $in\n);
open(IN, "<$in") || die("can't open $in: $!");
while (<IN>) {
($e, $n, $data[$ct]) = split " ", $_;
$data_sum += $data[$ct++];
}
@data_s = sort{$b <=> $a} @data;
$num = @data_s;
print LOG qq(Sorted $num items.\n);
my $sum = 0;
$ct = 0;
while ($sum < ($data_sum*.05) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
print CONTOURS qq($data_s[$ct-1] \n);
$q05 = $data_s[$ct-1];
while ( $sum < ($data_sum*.16) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
print CONTOURS qq($data_s[$ct-1] \n);
$q15 = $data_s[$ct-1];
while ( $sum < ($data_sum*.33) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
print CONTOURS qq($data_s[$ct-1] \n);
$q33 = $data_s[$ct-1];
while ( $sum < ($data_sum*.5) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
print CONTOURS qq($data_s[$ct-1] \n);
$q5 = $data_s[$ct-1];
while ( $sum < ($data_sum*.67) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
print CONTOURS qq($data_s[$ct-1] \n);
$q67 = $data_s[$ct-1];
while ( $sum < ($data_sum*.84) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
print CONTOURS qq($data_s[$ct-1] \n);
$q84 = $data_s[$ct-1];
while ( $sum < ($data_sum*.95) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
print CONTOURS qq($data_s[$ct-1] \n);
$q95 = $data_s[$ct-1];
while ( $sum < ($data_sum*.99) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
print CONTOURS qq($data_s[$ct-1] \n);
$q99 = $data_s[$ct-1];
print CPT qq(#\tcpt file created by: $0\n);
print CPT qq(#Color_MODEL = RGB\n);
print CPT qq(#\n);
printf CPT qq(%g\t%s\t%g\t%s\n), $q99, qq(255\t255\t255), $q95, qq(255\t255\t255\t;99th);
printf CPT qq(%g\t%s\t%g\t%s\n), $q95, qq(255\t255\t255), $q84, qq(0\t85\t170\t;95th);
printf CPT qq(%g\t%s\t%g\t%s\n), $q84, qq(0\t85\t170), $q67, qq(0\t170\t85\t;84th);
printf CPT qq(%g\t%s\t%g\t%s\n), $q67, qq(0\t170\t85), $q5, qq(0\t255\t0\t;67th);
printf CPT qq(%g\t%s\t%g\t%s\n), $q5, qq(0\t255\t0), $q33, qq(255\t255\t0\t;50th);
printf CPT qq(%g\t%s\t%g\t%s\n), $q33, qq(255\t255\t0), $q15, qq(255\t195\t0\t;33rd);
printf CPT qq(%g\t%s\t%g\t%s\n), $q15, qq(255\t195\t0), $q05, qq(255\t165\t0\t;15th);
printf CPT qq(%g\t%s\n\n), $q05, qq(255\t165\t0\t1\t255\t0\t0\t;5th);
printf CPT qq(%s\n),qq(B\t255\t255\t255);
printf CPT qq(%s\n), qq(F\t0\t0\t0);
printf CPT qq(%s\n), qq(N\t128\t128\t128);
}
elsif ($plot == 2 or $plot == 4) {
print LOG qq(opening $in; writing to $in.log\n);
system qq(perl convert2log.pl $in > $in.log);
$in = qq($in.log);
sleep(2);
open(IN, "<$in") || die("can't open $in: $!");
while (<IN>) {
($e, $n, $data[$ct]) = split " ", $_;
$data_sum += $data[$ct++];
}
@data_s = sort{$b <=> $a} @data;
$num = @data_s;
print STDERR qq("Sorted $num items.\n);
my $sum = 0;
$ct = 0;
while ($sum < ($data_sum*.05) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
#printf CONTOURS "%.5f\n", $data_s[$ct-1];
$q05 = $data_s[$ct-1];
print CONTOURS qq(-2\n);
$q05 = -2;
while ( $sum < ($data_sum*.16) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
#printf CONTOURS "%.5f\n", $data_s[$ct-1];
$q15 = $data_s[$ct-1];
print CONTOURS qq(-3\n);
$q15 = -3;
while ( $sum < ($data_sum*.33) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
#printf CONTOURS "%.5f\n", $data_s[$ct-1];
$q33 = $data_s[$ct-1];
print CONTOURS qq(-4\n);
$q33 = -4;
while ( $sum < ($data_sum*.5) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
#printf CONTOURS "%.5f\n", $data_s[$ct-1];
$q5 = $data_s[$ct-1];
print CONTOURS qq(-5\n);
$q5 = -5;
while ( $sum < ($data_sum*.67) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
#printf CONTOURS "%.5f\n", $data_s[$ct-1];
$q67 = $data_s[$ct-1];
print CONTOURS qq(-6\n);
$q67 = -6;
while ( $sum < ($data_sum*.84) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
#printf CONTOURS "%.5f\n", $data_s[$ct-1];
$q84 = $data_s[$ct-1];
print CONTOURS qq(-7\n);
$q84 = -7;
while ( $sum < ($data_sum*.95) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
#printf CONTOURS "%.5f\n", $data_s[$ct-1];
$q95 = $data_s[$ct-1];
print CONTOURS qq(-8\n);
$q95 = -8;
while ( $sum < ($data_sum*.99) ) {
$sum += $data_s[$ct++];
}
print LOG qq($data_s[$ct-1] a\n);
#printf CONTOURS "%.6f\n", $data_s[$ct-1];
$q99 = $data_s[$ct-1];
print CONTOURS qq(-9\n);
$q99 = -9;
printf CPT qq(%s\n\n), qq(# Spatial Density colormap\n# COLOR_MODEL = RGB);
#printf CPT "%.6f\t%s\t%.5f\t%s\n", $q99, "230\t230\t255", $q95, "230\t230\t255";
#printf CPT "%.5f\t%s\t%.5f\t%s\n", $q95, "0\t100\t200", $q84, "0\t100\t200";
#printf CPT "%.5f\t%s\t%.5f\t%s\n", $q84, "0\t170\t170", $q67, "0\t170\t170";
#printf CPT "%.5f\t%s\t%.5f\t%s\n", $q67, "0\t255\t0", $q5, "0\t255\t0";
#printf CPT "%.5f\t%s\t%.5f\t%s\n", $q5, "255\t255\t0", $q33, "255\t255\t0";
#printf CPT "%.5f\t%s\t%.5f\t%s\n", $q33, "255\t210\t0", $q15, "255\t210\t0";
#printf CPT "%.5f\t%s\t%.5f\t%s\n", $q15, "255\t165\t0", $q05, "255\t165\t0";
#printf CPT "%.5f\t%s\n\n", $q05, "235\t0\t0\t1\t235\t0\t0";
#print CPT "0 255 225 255 $q95 255 255 255\n";
#print CPT "$q95 255 255 255 $q84 0 85 170\n";
#print CPT "$q84 0 85 170 $q67 0 170 85\n";
#print CPT "$q67 0 170 85 $q5 0 255 0\n";
#print CPT "$q5 0 255 0 $q33 255 255 0\n";
#print CPT "$q33 255 255 0 $q15 255 195 0\n";
#print CPT "$q15 255 195 0 $q05 255 165 0\n";
#print CPT "$q05 255 165 0 1 255 0 0\n\n";
#print CPT "B 255 255 255\n";
#print CPT "F 255 0 0\n";
#print CPT "N 255 255 255\n";
print CPT qq(#\tcpt file created by: $0\n);
print CPT qq(#Color_MODEL = RGB\n);
print CPT qq(#\n);
printf CPT qq(%.1e\t%s\t%.1e\t%s\n), $q99, qq(230\t230\t255), $q95, qq(230\t230\t255\t;-2);
printf CPT qq(%.1e\t%s\t%.1e\t%s\n), $q95, qq(0\t100\t200), $q84, qq(0\t100\t200\t;-3);
printf CPT qq(%.1e\t%s\t%.1e\t%s\n), $q84, qq(0\t170\t170), $q67, qq(0\t170\t170\t;-4);
printf CPT qq(%.1e\t%s\t%.1e\t%s\n), $q67, qq(0\t255\t0), $q5, qq(0\t255\t0\t;-5);
printf CPT qq(%.1e\t%s\t%.1e\t%s\n), $q5, qq(255\t255\t0), $q33, qq(255\t255\t0\t;-6);
printf CPT qq(%.1e\t%s\t%.1e\t%s\n), $q33, qq(255\t210\t0), $q15, qq(255\t210\t0\t;-7);
printf CPT qq(%.1e\t%s\t%.1e\t%s\n), $q15, qq(255\t165\t0), $q05, qq(255\t165\t0\t;-8);
printf CPT qq(%.1e\t%s\n\n), $q05, qq(235\t0\t0\t0\t235\t0\t0\t;-9);
printf CPT qq(%s\n),qq(B\t255\t255\t255);
printf CPT qq(%s\n), qq(F\t0\t0\t0);
printf CPT qq(%s\n), qq(N\t128\t128\t128);
}
close CONTOURS;
close IN;
close CPT;
if ($plot == 1 or $plot == 2) {
# Convert to long / lat
my $proj = Geo::Proj4->new(proj => qq(utm), ellps => qq(WGS84), datum => qq(WGS84), zone => $utm_zone );
my ($south, $west) = $proj->inverse($utm_west, $utm_south);
my ($north, $east) = $proj->inverse($utm_east, $utm_north);
# .009259259 degree spacing for 100m
# .008333 degree for 90m
my $gs = 0.000092222 * $grid_spacing;
my $gs = (0.000092222 * $grid_spacing/4);
#$west = -123.0;
#$east = -120.6;
#$south = 40.0;
#$north = 41.55;
print STDERR qq(WEST $west\nEAST $east\nSOUTH $south\nNORTH $north\n);
system qq(proj -I +datum=WGS84 +ellps=WGS84 +proj=utm +zone=$utm_zone -f %.6f $in > $in.ll);
$in .= qq(.ll);
system qq(proj -I +datum=WGS84 +ellps=WGS84 +proj=utm +zone=$utm_zone -f %.6f $events > $events.ll);
$out = qq($in.eps);
`gmt surface \`gmt gmtinfo -I- $in \` $in -Gsurface.grd -I$gs -V`;
`gmt makecpt -C$cpt -V > grid.cpt `;
`gmt grdimage surface.grd -R$west/$east/$south/$north -Cgrid.cpt -Jm$scale -E$res -X1i -Y1i -V -K -P > $out`;
`gmt grdcontour surface.grd -Ccontours -A- -S2 -Gn1 -W.5p,0 -Jm -R -V -K -O >> $out`;
`gmt pscoast -R -Jm -Dh -W.5p,100 -N1/1,255/255/255 -O -K -V >> $out`;
$events .= qq(.ll);
`gmt psxy $events -Jm -Sc.1c -R -Gwhite -W.25p,0 -N -O -K -V >> $out`;
if (length($aoi) > 3) {
`gmt psxy $aoi -Jm -Ss.1i -R -G0 -O -K -V >> $out`;
`gmt pstext $aoi -Jm -R -D0/-.14 -G0 -O -K -V >> $out`;
}
`gmt psbasemap --FORMAT_FLOAT_MAP=%0.1f -Jm -R -Bxa0.5 -Bya0.5 -BWSne -V -O -K >> $out`;
`gmt psscale -D$psscaleX/$psscaleY/2i/.2c -Cgrid.cpt -L -O -V >> $out`;
}
elsif ($plot == 3 or $plot ==4) {
$gs = $grid_spacing;
$out = qq($in.eps);
`gmt xyz2grd \`gmt gmtinfo -I- $in \` $in -I$gs -Gsurface.grd -V`;
`gmt makecpt -C$cpt -V > grid.cpt `;
`gmt grdimage surface.grd -R$utm_west/$utm_east/$utm_south/$utm_north -Cgrid.cpt -Jx$scale -E$res -X1i -Y1i -V -K -P > $out`;
`gmt grdcontour surface.grd -Ccontours -A- -S2 -Gn1 -W.5p,0 -Jx -R -V -K -O >> $out`;
`gmt psxy $events -Jx -Sc.1c -R -Gwhite -W.25p,0 -N -O -K -V >> $out`;
if (length($aoi) > 3) {
`gmt psxy $aoi -Jm -Ss.1i -R -G0 -O -K -V >> $out`;
`gmt pstext $aoi -Jm -R -D0/-.14 -G0 -O -K -V >> $out`;
}
#-B option. Correct syntax:
#-B[p|s][x|y|z]<intervals>[+l<label>][+p<prefix>][+u<unit>] -B[<axes>][+b][+g<fill>][+o<lon>/<lat>][+t<title>] OR
#-B[p|s][x|y|z][a|f|g]<tick>[m][l|p] -B[p|s][x|y|z][+l<label>][+p<prefix>][+u<unit>] -B[<axes>][+b][+g<fill>][+o<lon>/<lat>][+t<title>]
`gmt psbasemap --FORMAT_FLOAT_OUT=%0f -Jx -R -Bxa20000 -Bya20000 -BWSne -V -O -K >> $out`;
`gmt psscale -D$psscaleX/$psscaleY/2i/.2c -L -Cgrid.cpt -O -V >> $out`;
}
`gmt ps2raster $out -A -Tg -V`;
`gmt ps2raster $out -A -Tf -V`;
exit(0);
# Only add DEM if grd file exists
unless (-e $grd) {
# Only process intensity file if it does not exist
unless ( -e $int) {
`gmt grdgradient $grd -G$int -R$west/$east/$south/$north -E-80/20/.5/.2/.2/100 -Nt0.5 -V `;
}
`gmt grdimage $grd -C$cpt -Jm$scale -I$int -R$west/$east/$south/$north -E$res -Ba50000 -BWSen -P -K > $out`;
}