Skip to content

Commit

Permalink
Check if the input of sample1d & filter1d have attributes and reprodu…
Browse files Browse the repository at this point in the history
…ce them on output if yes. (#1686)

* Test/demo file for the whittaker smoothing function.

* When passing -R=G only set -I if -I was not explicitly set.

* Do not reset opt_T if it's not empty.

* Fix bugs in days=float. Let list accept a country name.

* Add a yeardecimal method to do real > DateTime

* A couple of cleanings.

* Check if the input of sample1d & filter1d have attributes and reproduce them on output if yes.
  • Loading branch information
joa-quim authored Mar 7, 2025
1 parent 5efd462 commit cd98f39
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/common_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function parse_R(d::Dict, cmd::String; O::Bool=false, del::Bool=true, RIr::Bool=
if (RIr)
if (isa(val, GItype))
opt_I = parse_I(d, "", [:I :inc :increment :spacing], "I", true)
(opt_I == "") && (cmd *= " -I" * arg2str(val.inc))::String
(opt_I == "") ? (cmd *= " -I" * arg2str(val.inc))::String : (cmd *= opt_I)
opt_r = parse_r(d, "")[2]
(opt_r == "") && (cmd *= " -r" * ((val.registration == 0) ? "g" : "p"))
else # Here we must parse the -I and -r separately.
Expand Down
9 changes: 7 additions & 2 deletions src/filter1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ function filter1d_helper(cmd0::String, arg1; kwargs...)
cmd = add_opt(d, cmd, "F", [:F :filter :filter_type], (type="1", width="", highpass="_"))
end

(isvector(arg1)) && (arg1 = cat_1_arg(arg1)) # Accept vectors (GMT should do that too)
common_grd(d, cmd0, cmd, "filter1d ", arg1) # Finish build cmd and run it
(isvector(arg1)) && (arg1 = cat_1_arg(arg1)) # Accept vectors (GMT should do that too)
r = common_grd(d, cmd0, cmd, "filter1d ", arg1) # Finish build cmd and run it
if (isa(arg1, GDtype)) # Keep the attribs if they existed in arg1
isa(arg1, GMTdataset) && !isempty(arg1.attrib) && (r.attrib = arg1.attrib)
isa(arg1, Vector) && !isempty(arg1[1].attrib) && [r[k].attrib = arg1[1].attrib for k = 1:length(arg1)]
end
return r
end
6 changes: 3 additions & 3 deletions src/gmt_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1700,9 +1700,9 @@ info(C::GMTcpt) = show(C)
Base.:show(io::IO, mime::MIME"image/png", wp::WrapperPluto) = write(io, read(wp.fname))

# ---------- For Tables -----------------------------------------------------------------------------
Base.:names(D::GDtype) = isa(D, Vector) ? D[1].colnames : D.colnames
#Base.:names(D::GMTdataset) = D.colnames
#Base.:names(D::Vector{<:GMTdataset}) = D[1].colnames
#Base.:names(D::GDtype) = isa(D, Vector) ? D[1].colnames : D.colnames
Base.:names(D::GMTdataset) = D.colnames
Base.:names(D::Vector{<:GMTdataset}) = D[1].colnames

# ---------- For fck stop printing UInts in hexadecinal ---------------------------------------------
#Base.show(io::IO, x::T) where {T<:Union{UInt, UInt128, UInt64, UInt32, UInt16, UInt8}} = Base.print(io, x)
5 changes: 3 additions & 2 deletions src/gmtreadwrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function gmtread(_fname::String; kwargs...)
elseif ((opt_T = guess_T_from_ext(fname)) == " -Tg") fname *= "=gd"
end
else
opt_T = add_opt(d, "", "Ti", [:img :image])
(opt_T == "") && (opt_T = add_opt(d, "", "Ti", [:img :image]))
end

if (opt_T == "") opt_T = add_opt(d, "", "Td", [:data :dataset :table]) end
Expand Down Expand Up @@ -425,8 +425,9 @@ function file_has_time!(fname::String, D::GDtype, corder::Vector{Int}=Int[], opt
isone ? (D.colnames = String[]) : [D[k].colnames = String[] for k = 1:lastindex(D)]
@warn("Failed to parse file '$fname' for file_has_time!(). Error was:\n $err")
end
isone ? (length(D.colnames) < n_cols && (D.colnames = String[])) : (length(D[1].colnames) < n_cols && (D[1].colnames = String[]))
close(fid)
isone ? (length(D.colnames) < n_cols && (D.colnames = String[])) : (length(D[1].colnames) < n_cols && (D[1].colnames = String[]))
!isone && !isempty(D[1].attrib) && [D[k].attrib = D[1].attrib for k = 2:lastindex(D)] # All segs must have same attrib (?)
return nothing
end

Expand Down
17 changes: 13 additions & 4 deletions src/maregrams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ a valid one, we still return the data for that station. Example:
This is an alternative way of selecting a station instead of using `code` or `name` that require knowing them.
### Kwargs
- `list::Bool`: If true, returns a GMTdataset with the list all available stations and their codes and coords.
- `list`: If equal to `true`, returns a GMTdataset with the list all available stations and their codes and coords.
If it is a string, search all entries that contain that string. _e.g.,_ `list=Canada` returns all stations in Canada.
- `code`: Station code (See the output of `list`)
- `name`: In alternative to `code` give the station name (See the output of `list`)
- `days`: Number of days to be downloaded. It can be a decimal number.
Expand All @@ -39,8 +40,14 @@ D = maregrams(code="lgos", days=4, starttime="2025-02-01")
viz(D, title="Tide Gauge at Lagos (Portugal)")
```
"""
function maregrams(; list::Bool=false, code="", name="", days=2, starttime::String="", printurl::Bool=false)::GMTdataset
(list == true) && return gmtread(TESTSDIR * "/assets/maregs_online.csv")
function maregrams(; list=false, code="", name="", days=2, starttime::String="", printurl::Bool=false)::GMTdataset{Float64, 2}
if (list != 0) # Either list all stations or search those that contains a "list" string
Dl = gmtread(TESTSDIR * "/assets/maregs_online.csv")
list == 1 && return Dl
inds = findall(contains.(Dl.text,list))
isempty(inds) && (println("'$list' was not found in stations list."); return GMTdataset())
return mat2ds(Dl, (inds,:))
end
@assert days > 0 "Number of days must be > 0."
_code::String = string(code) # To let 'code' be a symbol as well
d = read_maregrams()
Expand All @@ -52,7 +59,9 @@ function maregrams(; list::Bool=false, code="", name="", days=2, starttime::Stri
# The shit here is that the site wants the endtime instead of starttime, sowe must do the maths to get it right.
(starttime != "") &&
try DateTime(starttime) catch; error("The start time '$starttime' is not a valid date.") end
endtime = (starttime == "") ? DateTime(now()) : DateTime(starttime) + Dates.Day(days)
M = ((daydec = getdecimal(days)) != 0) ? round(Int, daydec*24*60) : 0
(daydec != 0) && (days = trunc(Int, days))
endtime = (starttime == "") ? DateTime(now()) : DateTime(starttime) + Dates.Day(days) + Dates.Minute(M)
endtime > DateTime(now()) && (days -= round((endtime - DateTime(now())).value / (24*3600000), digits=6); endtime = DateTime(now()))
url = "http://www.ioc-sealevelmonitoring.org/bgraph.php?code=$_code&output=asc&period=$days&endtime=$endtime"
printurl && println(url[1:(54+length(_code))]) # Station's URL
Expand Down
3 changes: 1 addition & 2 deletions src/sample1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
Resample 1-D table data using splines
See full GMT (not the `GMT.jl` one) docs at [`sample1d`]($(GMTdoc)sample1d.html)
Parameters
----------
Expand Down Expand Up @@ -101,6 +99,7 @@ function sample1d_helper(cmd0::String, arg1; kwargs...)
if isa(arg1, GDtype)
colnames = isa(arg1, GMTdataset) ? arg1.colnames : arg1[1].colnames
have_cumdist && append!(colnames, ["cumdist"])
!isempty(arg1.attrib) && (r.attrib = arg1.attrib) # Keep the attribs
else # Input was eith a matrix or a file name
nc = isa(r, GMTdataset) ? size(r, 2) : size(r[1], 2)
colnames = [@sprintf("Z%d", k) for k = 1:nc]
Expand Down
13 changes: 11 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Collect generic utility functions in this file

""" Return the decimal part of a float number `x`"""
getdecimal(x::AbstractFloat) = x - trunc(Int, x)
getdecimal(x::Number) = x - trunc(Int, x)

""" Return an ierator over data skipping non-finite values"""
skipnan(itr) = Iterators.filter(el->isfinite(el), itr)
Expand Down Expand Up @@ -353,7 +353,8 @@ date2doy(date::String) = dayofyear(Date(date))
"""
yeardecimal(date)
Convert a Date or DateTime or a string representation of them to decimal years.
Convert a Date or DateTime or a string representation of them to decimal years, or vice-versa.
That is, convert from decimal year to DateTime.
### Example
yeardecimal(now())
Expand All @@ -375,6 +376,14 @@ function yeardecimal(dtm::Union{DateTime, Vector{DateTime}})
Y .+ frac
end

# This function is from DateFormats.jl
function yeardecimal(years::Real) # Covert from decimal years to DateTime
years_whole = floor(Int, years)
year_ms = DateTime(years_whole + 1) - DateTime(years_whole) |> Dates.value
period_ms = year_ms * (years - years_whole)
return DateTime(years_whole) + Millisecond(round(Int64, period_ms))
end

# --------------------------------------------------------------------------------------------------
function peaks(; N=49, grid::Bool=true, pixreg::Bool=false)
x,y = meshgrid(range(-3,stop=3,length=N))
Expand Down
6 changes: 2 additions & 4 deletions src/utils_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ function mat2ds(mat::Vector{<:AbstractMatrix}; hdr=String[], kwargs...)
d = KW(kwargs)
mat2ds(mat, hdr, d)
end
#function mat2ds(mat::Vector{<:AbstractMatrix}; hdr=String[], kwargs...)
function mat2ds(mat::Vector{<:AbstractMatrix}, hdr::Vector{String}, d::Dict)
#d = KW(kwargs)
D = Vector{GMTdataset{eltype(mat[1]), 2}}(undef, length(mat))
pen = find_in_dict(d, [:pen])[1]
color = find_in_dict(d, [:lc :linecolor :color])[1]
Expand Down Expand Up @@ -201,7 +199,8 @@ the georeference info as well as `attrib` and `colnames`.
mat2ds(mat::Array{T,N}, ref::GMTdataset) where {T,N} = mat2ds(mat; ref=ref)

# ---------------------------------------------------------------------------------------------------
function mat2ds(mat::Array{T,N}, txt::Union{String,Vector{String}}=String[]; hdr::Union{String,VecOrMat{String}}=String[], geom=0, kwargs...)::GDtype where {T,N}
function mat2ds(mat::Array{T,N}, txt::Union{String,Vector{String}}=String[];
hdr::Union{String,VecOrMat{String}}=String[], geom=0, kwargs...)::GDtype where {T,N}
d = KW(kwargs)
_mat2ds(mat, txt, isa(hdr, String) ? [hdr] : vec(hdr), Int(geom), d)
end
Expand Down Expand Up @@ -612,7 +611,6 @@ function ds2ds(D::GMTdataset; is3D::Bool=false, kwargs...)::Vector{<:GMTdataset}
d = KW(kwargs)
ds2ds(D, is3D, d)
end
#function ds2ds(D::GMTdataset; is3D::Bool=false, kwargs...)::Vector{<:GMTdataset}
function ds2ds(D::GMTdataset, is3D::Bool, d::Dict)::Vector{<:GMTdataset}
# Take one DS and split it into an array of DS's, one for each row and optionally add -G<fill>
# Alternativelly, if [:multi :multicol] options lieve in 'd', split 'D' by columns: [1,2], [1,3], [1,4], ...
Expand Down
2 changes: 1 addition & 1 deletion test/test_maregrams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

println(" MAREGRAMS")

maregrams(list=true);
maregrams(list="Portugal");
maregrams(-25, 36);
end

0 comments on commit cd98f39

Please sign in to comment.