Skip to content

Commit 6bb5fa0

Browse files
authored
Workaround the fact that GMT is not able to return a gmtconverted time. (#1862)
1 parent 10692fc commit 6bb5fa0

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/common_options.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,8 +1534,11 @@ function parse_f(d::Dict, cmd::String)
15341534
cmd, opt_f = parse_helper(cmd, d, [:f :geog :colinfo :coltypes :coltype], " -f")
15351535
if contains(opt_f, '/') # Case when -fi and -fo were both set
15361536
s = split(opt_f, '/')
1537-
new_f = s[1] * " -f" * s[2]
1537+
s1 = s[1][1] == 'i' ? s[1] : "i" * s[1] # Allow setting, or not, the i|o
1538+
s2 = s[2][1] == 'o' ? s[2] : "o" * s[2]
1539+
new_f = s1 * " -f" * s2
15381540
cmd = replace(cmd, opt_f => new_f)
1541+
opt_f = new_f
15391542
end
15401543
return cmd, opt_f
15411544
end

src/gmtconvert.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,31 @@ end
6262
# ---------------------------------------------------------------------------------------------------
6363
function gmtconvert_helper(cmd0::String, arg1, d::Dict{Symbol,Any})
6464

65-
cmd, = parse_common_opts(d, "", [:V_params :write :append :a :b :bo :d :e :f :g :h :i :o :q :s :w :yx])
65+
cmd, = parse_common_opts(d, "", [:V_params :append :a :b :bo :d :e :g :h :i :o :q :s :w :yx])
66+
cmd, opt_f = parse_f(d, cmd) # Must have easier acces to opt_f
67+
68+
# GMT is not able to return a converted time table, so we create a temporary file if time conversions are involved
69+
used_tmp_file = false
70+
if (contains(opt_f, "-fo") && is_in_dict(d, [:write :savefile :|>]) === nothing)
71+
s = split(opt_f)[end]
72+
if (contains(s, 'T') || contains(s, 't'))
73+
fname = TMPDIR_USR.dir * "/" * "GMTjl_time_" * TMPDIR_USR.username * TMPDIR_USR.pid_suffix * ".txt"
74+
d[:write] = fname
75+
used_tmp_file = true
76+
end
77+
end
78+
79+
cmd = parse_write(d, cmd)
6680
cmd = parse_these_opts(cmd, d, [[:A :hcat], [:C :n_records], [:D :dump], [:E :first_last], [:F :conn_method],
6781
[:I :invert :reverse], [:L :list_only], [:N :sort], [:Q :segments], [:S :select_hdr], [:T :suppress :skip], [:W :word2num], [:Z :transpose]])
6882

83+
6984
out = common_grd(d, cmd0, cmd, "gmtconvert ", arg1) # Finish build cmd and run it
70-
(!contains(cmd, " -b") && isa(out, GDtype) && cmd0 != "" && guess_T_from_ext(cmd0) == " -Td") && file_has_time!(cmd0, out) # Try to guess if time columns
85+
if (used_tmp_file) # Read the conversion results and clean up temporary file.
86+
out = gmtread(fname)
87+
rm(fname, force=true)
88+
else
89+
(!contains(cmd, " -b") && isa(out, GDtype) && cmd0 != "" && guess_T_from_ext(cmd0) == " -Td") && file_has_time!(cmd0, out) # Try to guess if time columns
90+
end
7191
return out
7292
end

test/test_B-GMTs.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666
println(" GMTCONVERT")
6767
gmtconvert([1.1 2; 3 4], o=0)
6868

69+
result = sample1d([0 0; 4 1], inc=0.5);
70+
D1 = gmtconvert(result, f="i0t/o0T", par=(TIME_EPOCH="1969-07-21T02:56:00", TIME_UNIT=:d));
71+
gmtconvert(result, f="i0t/o0T", par=(TIME_EPOCH="1969-07-21T02:56:00", TIME_UNIT=:d), savefile="tmp.txt")
72+
D2 = gmtread("tmp.txt");
73+
@test all(D1.data .== D2.data)
74+
rm("tmp.txt")
75+
6976
println(" GMTGRAVMAG3D")
7077
gmtgravmag3d(M=(shape=:prism, params=(1,1,1,5)), I=1.0, R="-15/15/-15/15", H="10/60/10/-10/40", Vd=dbg2);
7178
#D = gravmag3d(region="-15/15/-15/15", I=0.1, mag_params="10/60/10/-10/40", body=(shape=:prism, params="1/1/1/-5/-10/1"), F=[-14 -14; 14 14]);

test/test_misc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
GMT.GMT_Get_Default(API, "API_VERSION", " ");
201201
D = gmtconvert([1.0 2 3; 2 3 4], a="2=lolo+gPOINT"); # There's a bug in GMT for this. No data points are printed
202202
gmtwrite("lixo.gmt", D)
203-
@test gmtconvert([1.0 2 3; 2 3 4], binary_out="3f", write="a.bin", Vd=2) == "gmtconvert > a.bin -bo3f"
203+
@test gmtconvert([1.0 2 3; 2 3 4], binary_out="3f", write="a.bin", Vd=2) == "gmtconvert -bo3f > a.bin"
204204
@test gmtconvert([1.0 2 3; 2 3 4], binary_out="3f", append="a.bin", Vd=2) == "gmtconvert >> a.bin -bo3f"
205205
rm("lixo.gmt")
206206

0 commit comments

Comments
 (0)