Skip to content

Commit 7893fa8

Browse files
authored
Add new get_my_lonlat() function. Optionally export dataset in density(). (#1884)
1 parent 3dc1d24 commit 7893fa8

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/statplots.jl

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
"""
2-
D = density(x::Vector{<:Real}; nbins::Integer=200, bins::Vector{<:Real}=Vector{Real}(),
3-
bandwidth=nothing, kernel::StrSymb="normal")
2+
D = density(x; first::Bool=true, nbins::Integer=200, bins::Vector{<:Real}=Vector{Real}(),
3+
bandwidth=nothing, kernel::StrSymb="normal", printbw::Bool=false,
4+
extend=0, plot::Bool=true, kwargs...)
45
5-
- `x`: calculate the kernel density 'd' of a dataset X for query points 'xd' The density, by default, is
6-
estimated using a gaussian kernel with a width obtained with the Silverman's rule. `x` may be
7-
a vector, a matrix or Vector{Vector{Real}}.
8-
- `nbins`: points are queried between MIN(Y[:]) and MAX(Y[:]) where Y is the data vector.
6+
Calculate the kernel density 'd' of a dataset X at query points determined by `nbins` or `bins`.
7+
8+
- `x`: A vector, a matrix or Vector{Vector{Real}}. The density, by default, is estimated using a
9+
gaussian kernel with a width obtained with the Silverman's rule.
10+
- `nbins`: points are queried between MIN(X[:]) and MAX(X[:]) where X is the data vector.
911
- `bins`: Calculates the density for the query points specified by BINS. The values are used as the
1012
query points directly. Default is 200 points.
1113
- `bandwidth`: uses the 'bandwidth' to calculate the kernel density. It must be a scalar.
1214
For the uniform case the bandwidth is set to 15% of the range, otherwise the bandwidth is chosen
1315
with the Silverman's rule.
14-
- `printbw`: Logical value indicating to print the computed value of the `bandwidth`.
1516
- `kernel`: Uses the kernel function specified by KERNEL name (a string or a symbol) to calculate the density.
1617
The kernel may be: 'Normal' (default) or 'Uniform'
18+
- `printbw`: Logical value indicating to print the computed value of the `bandwidth`.
1719
- `extend`: By default the density curve is computed at the `bins` locatins or between data extrema as
1820
mentioned above. However, this is not normally enough to go down to zero. Use this option in terms of
1921
number of bandwidth to expand de curve. *e.g.* `extend=2`
22+
- `plot`: Logical value indicating to plot the density curve. If `false` returns the GMTdataset
23+
- `kwargs`: Any keyword argument accepted by `plot` to customize the density plot.
24+
25+
### Returns
26+
- `nothing` or a `GMTps` and plots the density curve directly if `plot=true` (the fedault) and shows it is `show=true`.
27+
- `D`: A GMTdataset with two columns: first the query points and second the density values.
2028
2129
The version
30+
2231
G = density(data, x,y; weights=nothing, bandwidth=nothing, showbw=false)
2332
2433
Computes the smoothed kernel probability density estimate of a two-column matrix `data`. The estimate is
@@ -32,10 +41,11 @@ The `showbw` option is used to print the bandwidth used. It returns a GMTgrid of
3241
viz(G, figsize=(12,12), view=(225,30))
3342
```
3443
"""
35-
function density(x; first::Bool=true, nbins::Integer=200, bins::Vector{<:Real}=Vector{Real}(), bandwidth=nothing,
36-
kernel::StrSymb="normal", printbw::Bool=false, horizontal::Bool=false, extend=0, kwargs...)
44+
function density(x; first::Bool=true, nbins::Integer=200, bins::Vector{<:Real}=Vector{Real}(),
45+
bandwidth=nothing, kernel::StrSymb="normal", printbw::Bool=false, horizontal::Bool=false,
46+
extend=0, plot::Bool=true, kwargs...)
3747
D = kernelDensity(x, horizontal; nbins=nbins, bins=bins, bandwidth=bandwidth, kernel=kernel, printbw=printbw, ext=extend)
38-
common_plot_xyz("", D, "line", first, false; kwargs...)
48+
return plot ? common_plot_xyz("", D, "line", first, false; kwargs...) : D
3949
end
4050
density!(x; nbins::Integer=200, bins::Vector{<:Real}=Vector{Real}(), bandwidth=nothing, kernel::StrSymb="normal", printbw::Bool=false, horizontal::Bool=false, extend=0, kw...) = density(x; first=false, nbins=nbins, bins=bins, bandwidth=bandwidth, kernel=kernel, printbw=printbw, horizontal=horizontal, extend=extend, kw...)
4151

src/utils.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ end
429429
Set the time column of a `D` GMTdataset (or vector of GMTdatasets) to a given time system.
430430
431431
### Args
432-
- `D``: a GMTdataset or a vector of GMTdatasets
432+
- `D`: a GMTdataset or a vector of GMTdatasets
433433
434434
### Kwargs
435435
- `col`: is the column number of the time column. Attention, this is a MANDATORY option
@@ -1182,9 +1182,26 @@ function whereami()
11821182
timezone = string(split(s[i_tz],":")[2][2:end-1])
11831183
region = string(split(s[i_region],":")[2][2:end-1])
11841184
ip = string(split(s[i_query],":")[2][2:end-2])
1185+
write(joinpath(GMTuserdir[1], "my_lonlat.dat"), "$lon $lat") # Save for usage from other functions
11851186
mat2ds([lon lat], colnames=["Lon","Lat"], attrib=Dict("Country" => country, "City" => city, "Region" => region, "Zip" => zip, "Timezone" => timezone, "IP" => ip))
11861187
end
11871188

1189+
# ------------------------------------------------------------------------------------------------------
1190+
"""
1191+
lon, lat = get_my_lonlat() -> Tuple{Float64, Float64}
1192+
1193+
Get the longitude and latitude saved by `whereami()`.
1194+
1195+
If the file does not exist, it calls `whereami()` to create it. File is saved in
1196+
`GMTuserdir[1]/my_lonlat.dat` (~/.gmt/my_lonlat.dat).
1197+
"""
1198+
function get_my_lonlat()::Tuple{Float64, Float64}
1199+
f = joinpath(GMTuserdir[1], "my_lonlat.dat")
1200+
(!isfile(f)) && whereami()
1201+
lon, lat = parse.(Float64, split(read(f, String)))
1202+
return lon, lat
1203+
end
1204+
11881205
# ------------------------------------------------------------------------------------------------------
11891206
# From this old SO post https://stackoverflow.com/questions/20484581/search-for-files-in-a-folder
11901207
"""
@@ -1398,7 +1415,6 @@ function settimecol!(D::GDtype, Tc::VecOrMat{<:Int})
13981415
isa(D, Vector) ? (D[1].attrib["Timecol"] = join(Tc, ",")) : (D.attrib["Timecol"] = join(Tc, ","))
13991416
return nothing
14001417
end
1401-
const set_timecol! = settimecol!
14021418

14031419
# ------------------------------------------------------------------------------------------------------
14041420
"""

0 commit comments

Comments
 (0)