21
|
1 |
#======================================================================
|
|
2 |
# L I B G M T . P L
|
|
3 |
# doc: Sun Jun 14 13:45:47 2015
|
27
|
4 |
# dlm: Thu Mar 17 12:19:42 2016
|
21
|
5 |
# (c) 2015 A.M. Thurnherr
|
27
|
6 |
# uE-Info: 45 48 NIL 0 0 72 2 2 4 NIL ofnI
|
21
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# perl implementation of /Data/Makefiles/Makefile.GMT
|
|
10 |
|
22
|
11 |
#----------------------------------------------------------------------
|
|
12 |
# USAGE
|
|
13 |
#----------------------------------------------------------------------
|
|
14 |
#
|
|
15 |
# Basic Example
|
|
16 |
# -------------
|
|
17 |
# GMT_begin('temp_prof.ps','-JX10/-10','-R0/30/0/5000');
|
|
18 |
# GMT_psxy('-W1,red');
|
|
19 |
# print(GMT "$temp $depth\n");
|
|
20 |
# GMT_end('-Ba5f1:"Temperature [degC]":/a500f100:"Depth [m]":WeSn');
|
|
21 |
#
|
|
22 |
# Other GMT Utilities
|
|
23 |
# -------------------
|
|
24 |
# GMT_pstext(<opts>) x y size angle fontno justify(ML,BC,TR,...) "text"
|
|
25 |
# GMT_psbasemap(<opts>) often implies GMT_end() w/o args
|
|
26 |
# GMT_psscale(<opts>) scale bar
|
|
27 |
#
|
|
28 |
# Other Extensions
|
|
29 |
# ----------------
|
|
30 |
# GMT_unitcoords(); afterwards, x and y range from 0 to 1; useful for legends
|
|
31 |
# GMT_setR('-R0/1/0/1') subsequent GMT utilities use this ROI
|
|
32 |
# GMT_setJ('-JX10/-1-') subsequent GMT utilities use this projection
|
|
33 |
# GMT_setAnnotFontSize(7) set primary annotation font size
|
|
34 |
#
|
|
35 |
#----------------------------------------------------------------------
|
|
36 |
|
21
|
37 |
# HISTORY:
|
|
38 |
# Jun 14, 2015: - created
|
|
39 |
# Jun 16, 2015: - BUG: forgot to return to PWD
|
|
40 |
# Jun 18, 2015: - added $DEBUG
|
22
|
41 |
# Jul 26, 2015: - added usage documentation
|
|
42 |
# - simplified GMT_unitcoords()
|
|
43 |
# Jul 28, 2015: - added GMT_setAnnotFontSize(), GMT_psscale()
|
27
|
44 |
# Mar 16, 2016: - adapted to GMT5
|
|
45 |
# Mar 17, 2016: - added check for gmt5 on load
|
21
|
46 |
|
22
|
47 |
$DEBUG = 0;
|
21
|
48 |
|
|
49 |
#----------------------------------------------------------------------
|
27
|
50 |
# Make sure gmt5 is installed
|
|
51 |
#----------------------------------------------------------------------
|
|
52 |
|
|
53 |
if (`which gmt` eq '') {
|
|
54 |
if (`which psxy` eq '') {
|
|
55 |
croak("$0: [libGMT.pl] gmt5 required, please install\n");
|
|
56 |
} else {
|
|
57 |
croak("$0: [libGMT.pl] gmt5 required (gmt4 installed on the system), please upgrade\n");
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
#----------------------------------------------------------------------
|
21
|
62 |
# Library
|
|
63 |
#----------------------------------------------------------------------
|
|
64 |
|
|
65 |
my($GMT_plotfile);
|
|
66 |
my($GMT_J);
|
|
67 |
my($GMT_R);
|
|
68 |
|
|
69 |
sub GMT_setR($) { ($GMT_R) = @_; } # (re-)define -R
|
|
70 |
sub GMT_setJ($) { ($GMT_J) = @_; } # (re-)define -J
|
|
71 |
|
|
72 |
sub GMT_spawn($) # spawn GMT command in temp dir
|
|
73 |
{
|
|
74 |
my($cmd) = @_;
|
|
75 |
close(GMT);
|
|
76 |
chdir("/tmp/antsGMT.$$") ||
|
|
77 |
croak("/tmp/antsGMT.$$: $!\n");
|
|
78 |
print(STDERR "$cmd\n") if ($DEBUG);
|
|
79 |
open(GMT,$cmd) || croak("$cmd: $!\n");
|
|
80 |
chdir("$ENV{PWD}") ||
|
|
81 |
croak("$ENV{PWD}: $!\n");
|
|
82 |
}
|
|
83 |
|
|
84 |
sub GMT_set(@) # set GMT defaults
|
|
85 |
{
|
26
|
86 |
GMT_spawn("| gmt set @_");
|
21
|
87 |
}
|
|
88 |
|
|
89 |
#----------------------------------------------------------------------
|
|
90 |
# GMT_begin(plot_file,J,R,extra_opts),
|
|
91 |
# e.g. GMT_begin('temp_prof.ps','-JX10/-10','-R0/30/0/5000');
|
|
92 |
# 1) create temp directory
|
|
93 |
# 2) set GMT defaults
|
|
94 |
# 3) create plot file with empty psxy
|
|
95 |
#----------------------------------------------------------------------
|
|
96 |
|
|
97 |
my($LABEL_FONT_SIZE) = 14 unless defined($LABEL_FONT_SIZE);
|
|
98 |
my($ANNOT_FONT_SIZE) = 14 unless defined($ANNOT_FONT_SIZE);
|
|
99 |
|
|
100 |
sub GMT_begin(@)
|
|
101 |
{
|
|
102 |
my($pfn,$J,$R,$extra) = @_;
|
|
103 |
mkdir("/tmp/antsGMT.$$");
|
|
104 |
chdir("/tmp/antsGMT.$$") ||
|
|
105 |
croak("/tmp/antsGMT.$$: $!\n");
|
|
106 |
system("rm -f .gmt*
|
26
|
107 |
gmt set PROJ_LENGTH_UNIT cm PS_MEDIA letter \\
|
|
108 |
FONT_LABEL ${LABEL_FONT_SIZE} \\
|
|
109 |
FONT_ANNOT_PRIMARY ${ANNOT_FONT_SIZE} \\
|
|
110 |
FORMAT_GEO_MAP ddd:mm:ssF") &&
|
|
111 |
croak("gmt set failed\n");
|
21
|
112 |
$GMT_plotfile = "$ENV{PWD}/$pfn";
|
|
113 |
GMT_setJ($J); GMT_setR($R);
|
26
|
114 |
GMT_spawn("| gmt psxy -K $J $R $extra > $GMT_plotfile");
|
21
|
115 |
close(GMT);
|
|
116 |
}
|
|
117 |
|
22
|
118 |
sub GMT_setAnnotFontSize($)
|
|
119 |
{
|
26
|
120 |
GMT_set("FONT_ANNOT_PRIMARY $_[0]");
|
22
|
121 |
}
|
|
122 |
|
21
|
123 |
#----------------------------------------------------------------------
|
|
124 |
# GMT_end(B)
|
|
125 |
# 1) chdir to temp-dir
|
|
126 |
# 2) psbasemap w/o -K
|
|
127 |
# 3) close GMT file
|
|
128 |
# 4) remove GMT temp dir
|
|
129 |
#----------------------------------------------------------------------
|
|
130 |
|
|
131 |
sub GMT_end(@)
|
|
132 |
{
|
|
133 |
my($opt) = @_;
|
26
|
134 |
if (defined($opt)) {
|
|
135 |
GMT_spawn("| gmt psbasemap -O $GMT_J $GMT_R $opt >> $GMT_plotfile");
|
|
136 |
} else {
|
|
137 |
GMT_spawn("| gmt psxy -O $GMT_J $GMT_R -Sc0.1 >> $GMT_plotfile");
|
|
138 |
}
|
21
|
139 |
close(GMT);
|
|
140 |
chdir("$ENV{PWD}") || croak("ENV{PWD}: $!\n");
|
|
141 |
system("rm -rf /tmp/antsGMT.$$") &&
|
|
142 |
croak("Offending command: rm -rf /tmp/antsGMT.$$\n");
|
|
143 |
}
|
|
144 |
|
|
145 |
#----------------------------------------------------------------------
|
|
146 |
# GMT_unitcoords()
|
|
147 |
# - set unit coordinate system
|
|
148 |
#----------------------------------------------------------------------
|
|
149 |
|
|
150 |
sub GMT_unitcoords()
|
|
151 |
{
|
22
|
152 |
GMT_setR('-R0/1/0/1');
|
|
153 |
}
|
|
154 |
|
|
155 |
sub GMT_unitcoords_logscale()
|
|
156 |
{
|
21
|
157 |
($jx,$jy) = ($GMT_J =~ m{-J.-?(\d+)[a-z]*/-?(\d+)});
|
|
158 |
if (defined($jy)) {
|
|
159 |
GMT_setJ("-JX$jx/$jy");
|
|
160 |
} else {
|
|
161 |
($jx) = ($GMT_J =~ m{-J.-?(\d+)});
|
|
162 |
if (defined($jx)) {
|
|
163 |
GMT_setJ("-JX$jx");
|
|
164 |
} else {
|
|
165 |
croak("cannot decode $GMT_J ($jx,$jy)");
|
|
166 |
}
|
|
167 |
}
|
|
168 |
GMT_setR('-R0/1/0/1');
|
26
|
169 |
# GMT_spawn("| gmt psxy -O -K $GMT_J $GMT_R >> $GMT_plotfile");
|
22
|
170 |
# close(GMT);
|
21
|
171 |
}
|
|
172 |
|
|
173 |
#----------------------------------------------------------------------
|
|
174 |
# GMT_psxy(opts)
|
|
175 |
# GMT_psbasemap(opts)
|
|
176 |
# GMT_pstext(opts)
|
22
|
177 |
# GMT_psscale(opts)
|
21
|
178 |
#----------------------------------------------------------------------
|
|
179 |
|
|
180 |
sub GMT_psxy(@)
|
|
181 |
{
|
|
182 |
my($opts) = @_;
|
26
|
183 |
GMT_spawn("| gmt psxy -O -K $GMT_J $GMT_R $opts >> $GMT_plotfile");
|
21
|
184 |
}
|
|
185 |
|
|
186 |
sub GMT_psbasemap(@)
|
|
187 |
{
|
|
188 |
my($opts) = @_;
|
26
|
189 |
GMT_spawn("| gmt psbasemap -O -K $GMT_J $GMT_R $opts >> $GMT_plotfile");
|
21
|
190 |
}
|
|
191 |
|
|
192 |
sub GMT_pstext(@)
|
|
193 |
{
|
|
194 |
my($opts) = @_;
|
26
|
195 |
GMT_spawn("| gmt pstext -O -K $GMT_J $GMT_R $opts >> $GMT_plotfile");
|
21
|
196 |
}
|
|
197 |
|
22
|
198 |
sub GMT_psscale(@)
|
|
199 |
{
|
|
200 |
my($opts) = @_;
|
26
|
201 |
GMT_spawn("| gmt psscale -O -K $GMT_J $GMT_R $opts >> $GMT_plotfile");
|
22
|
202 |
}
|
|
203 |
|
21
|
204 |
1;
|