From d491b80495dd25e541dca9763fc4d08a5f3ffd18 Mon Sep 17 00:00:00 2001 From: Mao Zhou Date: Sun, 29 Sep 2024 14:07:20 +1000 Subject: [PATCH] Replace sprintf with @sprintf and remove sprintf function --- src/common_options.jl | 36 ++++++++++++++++++------------------ src/gmtbegin.jl | 8 ++++---- src/grdimage.jl | 2 +- src/grdlandmask.jl | 10 +++++----- src/libgmt.jl | 18 +----------------- src/rasterpolygonfuns.jl | 2 +- 6 files changed, 30 insertions(+), 46 deletions(-) diff --git a/src/common_options.jl b/src/common_options.jl index fade5ddc3e..d278b20093 100644 --- a/src/common_options.jl +++ b/src/common_options.jl @@ -177,7 +177,7 @@ function parse_R(d::Dict, cmd::String, O::Bool=false, del::Bool=true, RIr::Bool= (opt_R != " -Rtight" && opt_R !== nothing && limits != zeros(4) && all(CTRL.limits[1:4] .== 0)) && (CTRL.limits[1:length(limits)] = limits) # And this makes data = plot limits, IF data is empty. if (istilename(opt_R)) # A XYZ or quadtree tile address (like "7829,6374,14") - opt_R = sprintf(" -R%.12g/%.12g/%.12g/%.12g", limits[1], limits[2], limits[3], limits[4]) + opt_R = @sprintf(" -R%.12g/%.12g/%.12g/%.12g", limits[1], limits[2], limits[3], limits[4]) elseif contains(opt_R, "+r") CTRL.limits[13] = 1.0 # To know that -R...+r was used. end @@ -217,7 +217,7 @@ function merge_R_and_xyzlims(d::Dict, opt_R::String)::String xlim, ylim, zlim = parse_lims(d, 'x'), parse_lims(d, 'y'), parse_lims(d, 'z') =# function fetch_xyz_lims(d::Dict, symbs)::String - return ((val = find_in_dict(d, symbs, false)[1]) !== nothing) ? sprintf("%.15g/%.15g", val[1], val[2]) : "" + return ((val = find_in_dict(d, symbs, false)[1]) !== nothing) ? @sprintf("%.15g/%.15g", val[1], val[2]) : "" end xlim::String = fetch_xyz_lims(d, [:xlim :xlims :xlimits]) @@ -282,7 +282,7 @@ function build_opt_R(val, symb::Symbol=Symbol())::String # Generic function tha R = " -R" * arg2str(val) end elseif (isa(val, GItype)) - R = sprintf(" -R%.15g/%.15g/%.15g/%.15g", val.range[1], val.range[2], val.range[3], val.range[4]) + R = @sprintf(" -R%.15g/%.15g/%.15g/%.15g", val.range[1], val.range[2], val.range[3], val.range[4]) elseif (isa(val, GDtype)) bb::Vector{<:Float64} = isa(val, GMTdataset) ? val.bbox : val[1].ds_bbox R = (symb ∈ (:region_llur, :limits_llur, :limits_diag, :region_diag)) ? @@ -332,7 +332,7 @@ function build_opt_R(arg::NamedTuple, symb::Symbol=Symbol())::String if (isa(val, String) || isa(val, Real)) t::String = string(val) elseif (isa(val, VecOrMat{<:Real}) || isa(val, Tuple)) - t = join([sprintf("%.15g/", Float64(x)) for x in val]) + t = join([@sprintf("%.15g/", Float64(x)) for x in val]) t = rstrip(t, '/') # and remove last '/' else error("Increments for limits must be a String, a Number, Array or Tuple") @@ -819,12 +819,12 @@ function parse_proj(p::NamedTuple)::Tuple{String, Bool} center::String = "" if ((val = find_in_dict(d, [:center])[1]) !== nothing) if (isa(val, String)) center = val - elseif (isa(val, Real)) center = sprintf("%.12g", val) + elseif (isa(val, Real)) center = @sprintf("%.12g", val) elseif (isa(val, Array) || isa(val, Tuple) && length(val) == 2) - if (isa(val, Array)) center = sprintf("%.12g/%.12g", val[1], val[2]) + if (isa(val, Array)) center = @sprintf("%.12g/%.12g", val[1], val[2]) else # Accept also strings in tuple (Needed for movie) - center = (isa(val[1], String)) ? val[1]::String * "/" : sprintf("%.12g/", val[1]) - center *= (isa(val[2], String)) ? val[2]::String : sprintf("%.12g", val[2]) + center = (isa(val[1], String)) ? val[1]::String * "/" : @sprintf("%.12g/", val[1]) + center *= (isa(val[2], String)) ? val[2]::String : @sprintf("%.12g", val[2]) end end end @@ -834,9 +834,9 @@ function parse_proj(p::NamedTuple)::Tuple{String, Bool} parallels::String = "" if ((val = find_in_dict(d, [:parallel :parallels])[1]) !== nothing) if (isa(val, String)) parallels = "/" * val - elseif (isa(val, Real)) parallels = sprintf("/%.12g", val) + elseif (isa(val, Real)) parallels = @sprintf("/%.12g", val) elseif (isa(val, Array) || isa(val, Tuple) && (length(val) <= 3 || length(val) == 6)) - parallels = join([sprintf("/%.12g",x)::String for x in val]) + parallels = join([@sprintf("/%.12g",x)::String for x in val]) end end @@ -2685,7 +2685,7 @@ function get_cpt_set_R(d::Dict, cmd0::String, cmd::String, opt_R::String, got_fn cpt_opt_T = "" if ((val = find_in_dict(d, [:clim])[1]) !== nothing) (!isa(val, StrSymb) && length(val) != 2) && error("The clim option is neither = 'zscale' nor a two elements with z_min, z_max") - cpt_opt_T = (isa(val, StrSymb)) ? sprintf(" -T%.12g/%.12g/256+n -D", zscale(arg1)...) : sprintf(" -T%.12g/%.12g/256+n -D", val[1], val[2]) + cpt_opt_T = (isa(val, StrSymb)) ? @sprintf(" -T%.12g/%.12g/256+n -D", zscale(arg1)...) : @sprintf(" -T%.12g/%.12g/256+n -D", val[1], val[2]) end if (isa(arg1, GItype) || (cmd0 != "" && cmd0[1] != '@')) @@ -2693,20 +2693,20 @@ function get_cpt_set_R(d::Dict, cmd0::String, cmd::String, opt_R::String, got_fn # If no cpt name sent in, then compute (later) a default cpt if (isa(arg1, GMTgrid) && ((val = find_in_dict(d, [:percent :pct])[1])) !== nothing) lh = quantile(any(!isfinite, arg1) ? skipnan(vec(arg1)) : vec(arg1), [(100 - val)/200, (1 - (100 - val)/200)]) - cpt_opt_T = sprintf(" -T%.12g/%.12g/256+n -D", lh[1], lh[2]) # Piggyback -D + cpt_opt_T = @sprintf(" -T%.12g/%.12g/256+n -D", lh[1], lh[2]) # Piggyback -D elseif ((val = find_in_dict(d, [:percent :pct])[1]) !== nothing) # Case of a grid file range = vec(grdinfo(cmd0 * " -C -T+a$(100-val)"::String).data); - cpt_opt_T = sprintf(" -T%.12g/%.12g/256+n -D", range[5], range[6]) + cpt_opt_T = @sprintf(" -T%.12g/%.12g/256+n -D", range[5], range[6]) elseif (cpt_opt_T == "") drange = range[6] - range[5] (drange > 1e6) && @warn("The z range expands to more then 6 orders of magnitude. Missed to replace the nodatavalues?\n\n") loc_eps = (drange > 1e-8) ? 1e-8 : 1e-15 # Totally ad hoc condition - cpt_opt_T = sprintf(" -T%.12g/%.12g/256+n", range[5] - loc_eps, range[6] + loc_eps) + cpt_opt_T = @sprintf(" -T%.12g/%.12g/256+n", range[5] - loc_eps, range[6] + loc_eps) end (range[5] > 1e100) && (cpt_opt_T = "") # cmd0 is an image name and now grdinfo does not compute its min/max end if (opt_R == "" && (!IamModern[1] || (IamModern[1] && FirstModern[1])) ) # No -R ovewrite by accident - cmd *= sprintf(" -R%.14g/%.14g/%.14g/%.14g", range[1], range[2], range[3], range[4]) + cmd *= @sprintf(" -R%.14g/%.14g/%.14g/%.14g", range[1], range[2], range[3], range[4]) end elseif (cmd0 != "" && cmd0[1] == '@') # No reason not to let @grids use clim=[...] if (any(contains.(cmd0, ["_01d", "_30m", "_20m", "_15m", "_10m", "_06m"])) && (val = find_in_dict(d, [:percent :pct])[1]) !== nothing) @@ -2886,7 +2886,7 @@ function get_color(val::Tuple)::String if (isa(val[k], Tuple) && (length(val[k]) == 3)) s = 1.0 if (val[k][1] <= 1 && val[k][2] <= 1 && val[k][3] <= 1) s = 255.0 end # colors in [0 1] - out *= sprintf("%.0f/%.0f/%.0f,", val[k][1]*s, val[k][2]*s, val[k][3]*s) + out *= @sprintf("%.0f/%.0f/%.0f,", val[k][1]*s, val[k][2]*s, val[k][3]*s) elseif (isa(val[k], Symbol) || isa(val[k], String) || isa(val[k], Real)) out *= string(val[k],",")::String else @@ -4415,7 +4415,7 @@ function finish_PS_module(d::Dict, cmd::Vector{String}, opt_extra::String, K::Bo if (!isgeog(proj4)) opt_J = replace(proj4, " " => "") isoblique = any(contains.(opt_J, ["=utm", "=lcc", "=omerc", "=tmerc", "=laea"])) # <== ADD OTHER OBLIQUES HERE - opt_R::String = isoblique ? sprintf(" -R%f/%f/%f/%f+r", WESN[1],WESN[3],WESN[2],WESN[4]) : sprintf(" -R%f/%f/%f/%f", WESN...) + opt_R::String = isoblique ? @sprintf(" -R%f/%f/%f/%f+r", WESN[1],WESN[3],WESN[2],WESN[4]) : @sprintf(" -R%f/%f/%f/%f", WESN...) size_::String = (J1[1] == 'x') ? "+scale=" * J1[2:end] : (J1[1] == 'X') ? "+width=" * J1[2:end] : "" (size_ == "") && @warn("Could not find the right fig size used. Result will be wrong") cmd[k] = replace(cmd[k], " -J" => " -J" * opt_J * size_) @@ -4425,7 +4425,7 @@ function finish_PS_module(d::Dict, cmd::Vector{String}, opt_extra::String, K::Bo if (J2 != "") # Case where J2 wasn't simply "-J" @warn("If $J1 is a cylindrical projection, second set of coordinates will likely be wrong.") D = mapproject([WESN[1] WESN[3]; WESN[1] WESN[4]; WESN[2] WESN[4]; WESN[2] WESN[3]], J=J2) - opt_R = sprintf(" -R%.12g/%.12g/%.12g/%.12g", D.ds_bbox...) + opt_R = @sprintf(" -R%.12g/%.12g/%.12g/%.12g", D.ds_bbox...) W = (CTRL.pocket_J[2] != "") ? CTRL.pocket_J[2] : string(split(DEF_FIG_SIZE, "/")[1]) fig_size = W * "/" * string(mapproject([WESN[1] WESN[4]], R=[WESN...], J=J1).data[2])::String cmd[k] = replace(cmd[k], J2 => islowercase(J2[1]) ? "x" * fig_size : "X" * fig_size) # Fails for scales diff --git a/src/gmtbegin.jl b/src/gmtbegin.jl index 65483a117a..9200b22484 100644 --- a/src/gmtbegin.jl +++ b/src/gmtbegin.jl @@ -155,7 +155,7 @@ function inset_nested(GI::GItype, n; kwargs...) CTRL.pocket_d[1][:J] = opt_J[4:end] if (opt_R == "") - opt_R = sprintf(" -R%.12g/%.12g/%.12g/%.12g", GI.range[1], GI.range[2], GI.range[3], GI.range[4]) # Get the current GI + opt_R = @sprintf(" -R%.12g/%.12g/%.12g/%.12g", GI.range[1], GI.range[2], GI.range[3], GI.range[4]) # Get the current GI CTRL.pocket_d[1][:R] = opt_R[4:end] end (opt_B == " -B0") && (opt_B = " -Bnone") # No frame border so that the inset box pen (-F) may take effect. @@ -175,7 +175,7 @@ function inset_nested(D::GDtype, n; kwargs...) (opt_J == "") && (d[:J] = "X?/?"; IamInset[2] = true) # IamInset[2] is to help avoid GMT bug #7005 if (opt_R == "") bb = getbb(D) - opt_R = sprintf(" -R%.12g/%.12g/%.12g/%.12g", bb...) + opt_R = @sprintf(" -R%.12g/%.12g/%.12g/%.12g", bb...) end d[:R] = opt_R[4:end] (opt_B != "") && (d[:B] = opt_B[4:end]) @@ -262,7 +262,7 @@ function helper1_inset_nested(d; iscoast=false, isplot=false, imgdims=tuple()) inset_H = inset_W * aspect_zoom if ((val = find_in_dict(d, [:pzoom])[1]) !== nothing) # A pseudo-zoom from a single point. val_f::Vector{Float64} = [Float64(val[1]), Float64(val[2])] - d[:R] = sprintf("%.15g/%.15g/%.15g/%.15g", val_f[1], val_f[1]*1.001, val_f[2], val_f[2]*1.001) + d[:R] = @sprintf("%.15g/%.15g/%.15g/%.15g", val_f[1], val_f[1]*1.001, val_f[2], val_f[2]*1.001) delete!(d, :pzoom) end (opt_J != "" && !contains(opt_J, '?')) && (opt_J = replace(opt_J, CTRL.pocket_J[2] => "?")) # Replace the fig's default size @@ -493,7 +493,7 @@ function zoom2inset(d, center) CTRL.pocket_call[4] = (nDS == 1) ? D[1] : D CTRL.limits[7:end] = bak # Reset the backed up values end - d[:R] = sprintf("%.15g/%.15g/%.15g/%.15g", zoom_lims...) + d[:R] = @sprintf("%.15g/%.15g/%.15g/%.15g", zoom_lims...) d[:Rzoom_num] = zoom_lims return nothing end diff --git a/src/grdimage.jl b/src/grdimage.jl index 9e3be5f8f6..880542d479 100644 --- a/src/grdimage.jl +++ b/src/grdimage.jl @@ -180,7 +180,7 @@ function common_insert_R!(d::Dict, O::Bool, cmd0, I_G; is3D=false) d[:R] = val end try # Can't risk to error here - opt_R = sprintf("%.15g/%.15g/%.15g/%.15g", d[:R][1], d[:R][2], d[:R][3], d[:R][4]) + opt_R = @sprintf("%.15g/%.15g/%.15g/%.15g", d[:R][1], d[:R][2], d[:R][3], d[:R][4]) catch end delete!(d, [:region, :limits]) diff --git a/src/grdlandmask.jl b/src/grdlandmask.jl index b6c8a2ea53..7d182eb65a 100644 --- a/src/grdlandmask.jl +++ b/src/grdlandmask.jl @@ -52,11 +52,11 @@ function grdlandmask_helper(cmd0::String, arg1; kwargs...) prj = getproj(arg1, proj4=true) (contains(prj, "=lon") || contains(prj, "=lat")) && (prj = "") # Cool, it's geog, no proj needed. if (prj == "") - d[:R] = sprintf("%.12g/%.12g/%.12g/%.12g", arg1.range[1:4]...) - d[:I] = sprintf("%.12g/%.12g", arg1.inc[1:2]...) + d[:R] = @sprintf("%.12g/%.12g/%.12g/%.12g", arg1.range[1:4]...) + d[:I] = @sprintf("%.12g/%.12g", arg1.inc[1:2]...) else t = xy2lonlat([arg1.range[1] 0; arg1.range[2] 0; 0 arg1.range[3]; 0 arg1.range[4]], s_srs=prj, t_srs="+proj=longlat +datum=WGS84") - d[:R] = sprintf("%.12g/%.12g/%.12g/%.12g", t[1, 1], t[2, 1], t[3, 2], t[4, 2]) + d[:R] = @sprintf("%.12g/%.12g/%.12g/%.12g", t[1, 1], t[2, 1], t[3, 2], t[4, 2]) height, width = dims(arg1) d[:I] = "$(width)" * "+n/" * "$(height)" * "+n" end @@ -69,7 +69,7 @@ function grdlandmask_helper(cmd0::String, arg1; kwargs...) r = common_grd(d, "grdlandmask " * cmd, nothing) # Finish build cmd and run it if (arg1 !== nothing && prj != "") # project the mask grid to be compatible with the original grid. r.z = reshape(r.z, width, height) - r = gdalwarp(r, ["-of","MEM","-t_srs",prj,"-ts","$(width)", "$(height)", "-te", sprintf("%.12g", arg1.range[1]), sprintf("%.12g", arg1.range[3]), sprintf("%.12g", arg1.range[2]), sprintf("%.12g", arg1.range[4])], layout=r.layout) # Many things can go wrong here. + r = gdalwarp(r, ["-of","MEM","-t_srs",prj,"-ts","$(width)", "$(height)", "-te", @sprintf("%.12g", arg1.range[1]), @sprintf("%.12g", arg1.range[3]), @sprintf("%.12g", arg1.range[2]), @sprintf("%.12g", arg1.range[4])], layout=r.layout) # Many things can go wrong here. end if (isa(arg1, GMTgrid)) if (r.hasnans == 2 || opt_N == "") r *= arg1 # If clipping val is NaN or 0 @@ -88,4 +88,4 @@ function grdlandmask_helper(cmd0::String, arg1; kwargs...) return I end return r -end \ No newline at end of file +end diff --git a/src/libgmt.jl b/src/libgmt.jl index 73e1355ec6..6b8ba03fa2 100644 --- a/src/libgmt.jl +++ b/src/libgmt.jl @@ -427,22 +427,6 @@ function gmt_free_mem(API::Ptr{Cvoid}, mem) ccall((:gmt_free_func, libgmt), Cvoid, (Cstring, Ptr{Cvoid}, Bool, Cstring), GMT_, mem, true, "Julia") end -function sprintf(format::String, x...) - strp = Ref{Ptr{Cchar}}(0) - if (length(x) == 1) - len = ccall(:asprintf, Cint, (Ptr{Ptr{Cchar}}, Cstring, Cdouble...), strp, format, x[1]) - elseif (length(x) == 2) - len = ccall(:asprintf, Cint, (Ptr{Ptr{Cchar}}, Cstring, Cdouble, Cdouble...), strp, format, x[1], x[2]) - elseif (length(x) == 3) - len = ccall(:asprintf, Cint, (Ptr{Ptr{Cchar}}, Cstring, Cdouble, Cdouble, Cdouble...), strp, format, x[1], x[2], x[3]) - elseif (length(x) == 4) - len = ccall(:asprintf, Cint, (Ptr{Ptr{Cchar}}, Cstring, Cdouble, Cdouble, Cdouble, Cdouble...), strp, format, x[1], x[2], x[3], x[4]) - end - str = unsafe_string(strp[],len) - Libc.free(strp[]) - return str -end - #= function get_common_R(API::Ptr{Cvoid}) R = COMMON_R((false,false,false,false), false, 0, 0, 0, (0., 0., 0., 0., 0., 0.), (0., 0., 0., 0.), (0., 0.), map(UInt8, (string(repeat(" ",256))...,))) @@ -462,4 +446,4 @@ fix_mercator(data, detail, nrows, ncols, lat1, lat2) = ccall((:fix_mercator, libgmt), Cvoid, (Ptr{Cfloat}, Cdouble, Cint, Cint, Cdouble, Cdouble), data, nrows, ncols, detail, lat1, lat2) fix_polar_stereographic(data, detail, nrows, ncols, center_res) = - ccall((:fix_polar_stereographic, libgmt), Cvoid, (Ptr{Cfloat}, Cdouble, Cint, Cint, Cdouble), data, nrows, ncols, detail, center_res) \ No newline at end of file + ccall((:fix_polar_stereographic, libgmt), Cvoid, (Ptr{Cfloat}, Cdouble, Cint, Cint, Cdouble), data, nrows, ncols, detail, center_res) diff --git a/src/rasterpolygonfuns.jl b/src/rasterpolygonfuns.jl index b15e519945..930e14d72d 100644 --- a/src/rasterpolygonfuns.jl +++ b/src/rasterpolygonfuns.jl @@ -274,7 +274,7 @@ function maskgdal(D::GDtype, nx, ny; region=Float64[], touches=false, layout::St local mask opts = ["-of", "MEM", "-ts","$(nx)","$(ny)", "-burn", "1", "-ot", "Byte"] (touches == 1) && append!(opts, ["-at"]) - !isempty(region) && append!(opts, ["-te", sprintf("%.12g", region[1]), sprintf("%.12g", region[3]), sprintf("%.12g", region[2]), sprintf("%.12g", region[4])]) + !isempty(region) && append!(opts, ["-te", @sprintf("%.12g", region[1]), @sprintf("%.12g", region[3]), @sprintf("%.12g", region[2]), @sprintf("%.12g", region[4])]) try mk = gdalrasterize(D, opts, layout=layout) # This may fail if the polygon is degenerated. mask = reinterpret(Bool, mk.image)