Skip to content

Commit

Permalink
Merge pull request #41 from JuliaClimate/improvs
Browse files Browse the repository at this point in the history
Minor various improvements
  • Loading branch information
Datseris authored Feb 16, 2021
2 parents 6458d75 + f10694e commit e1cb93e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
6 changes: 3 additions & 3 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ using ClimateBase # hide
ClimateBase.COMMONNAMES
```

Also, two convenience functions are provided for examining the content of on-disk `.nc` files without loading all data on memory.
Also, the following convenience functions are provided for examining the content of on-disk `.nc` files without loading all data on memory.
```@docs
nckeys
ncdetails
globalattr
```

### Write
Expand Down Expand Up @@ -114,8 +115,7 @@ function extract_dimension_values_xarray(xa, dnames = collect(xa.dims))
if d "time"
push!(dim_values, x)
else
# This date specification assumes up to day sampling (hence the 1:10)
dates = [np.datetime_as_string(y)[1:10] for y in x]
dates = [np.datetime_as_string(y)[1:19] for y in x]
dates = DateTime.(dates)
push!(dim_values, dates)
end
Expand Down
6 changes: 3 additions & 3 deletions src/core/coredefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Time = DimensionalData.Ti
AbDimArray = DimensionalData.AbstractDimArray

export At, Between, Near # Selectors from DimensionalArrays.jl
export hasdim, DimensionalArray
export get_var_as_dimarray
export hasdim, DimensionalArray, dimnum
export Time, Lon, Lat, dims, Coord, Hei, Pre
export EqArea, Grid, spacestructure
export DimensionalData # for accessing its functions

@dim Lon IndependentDim "Longitude"
@dim Lat IndependentDim "Latitude"
Expand Down Expand Up @@ -70,7 +70,7 @@ struct ClimArray{T,N,D<:Tuple,R<:Tuple,A<:AbstractArray{T,N},Me} <: AbstractDime
end
ClimArray(A::DimensionalArray) = ClimArray(A.data, A.dims, A.refdims, A.name, A.metadata)
ClimArray(A::ClimArray, dims::Tuple = A.dims; name = A.name, attrib = A.attrib) =
ClimArray(A.data, DimensionalData.formatdims(A.data, dims), A.refdims, name, attrib)
ClimArray(A.data, DimensionalData.formatdims(A.data, dims), A.refdims, Symbol(name), attrib)

"""
ClimArray(A::Array, dims::Tuple; name = "", attrib = nothing)
Expand Down
14 changes: 12 additions & 2 deletions src/core/nc_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ https://github.com/rafaqz/GeoData.jl
=#
using NCDatasets
export NCDataset
export nckeys, ncdetails
export nckeys, ncdetails, globalattr
export climarrays_to_nc

dim_to_commonname(::Lat) = "lat"
Expand Down Expand Up @@ -39,6 +39,16 @@ function ncdetails(file::String, io = stdout)
end
ncdetails(ds::NCDataset, io = stdout) = show(io, MIME"text/plain"(), ds)

"""
globalattr(file::String) → Dict
Return the global attributions of the .nc file.
"""
function globalattr(file::String)
NCDataset(file) do ds
return Dict(ds.attrib)
end
end

"""
ClimArray(file::Union{String,NCDataset}, var::String, name = var) -> A
Load the variable `var` from the `file` and convert it
Expand Down Expand Up @@ -198,7 +208,6 @@ function climarrays_to_nc(file::String, Xs; globalattr = Dict())
@warn "$i-th ClimArray has no name, naming it $(n) instead."
end
println("processing variable $(n)...")
println("writing dimensions...")
add_dims_to_ncfile!(ds, dims(X))
println("writing the CF-variable...")
attrib = X.attrib
Expand All @@ -215,6 +224,7 @@ function add_dims_to_ncfile!(ds::NCDatasets.AbstractDataset, dimensions::Tuple)
dnames = dim_to_commonname.(dimensions)
for (i, d) enumerate(dnames)
haskey(ds, d) && continue
println("writing dimension $d...")
v = dimensions[i].val
# this conversion to DateTime is necessary because CFTime.jl doesn't support Date
eltype(v) == Date && (v = DateTime.(v))
Expand Down
3 changes: 3 additions & 0 deletions src/physical_dimensions/spatial_equalarea.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function ClimArray_eqarea(ds::NCDatasets.AbstractDataset, var::String, name = va

X = ClimArray(A, Tuple(actualdims))
X = X[Coord(si)]
if !any(ismissing, X)
X = nomissing(X)
end
return ClimArray(X; name = Symbol(name), attrib)
else
error("Don't know how to handle this equal area grid!")
Expand Down
10 changes: 5 additions & 5 deletions src/physical_dimensions/temporal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ end
# Monthly/yearly/daily/seasonal means
#########################################################################
"""
monthlyagg(A::ClimArray, f = mean) -> B
monthlyagg(A::ClimArray, f = mean; mday = 15) -> B
Create a new array where the temporal daily information has been aggregated to months
using the function `f`.
By convention, the dates of the new array always have day number of `15`.
The dates of the new array always have day number of `mday`.
"""
function monthlyagg(A::ClimArray, f = mean)
function monthlyagg(A::ClimArray, f = mean; mday = 15)
t0 = dims(A, Time).val
startdate = Date(year(t0[1]), month(t0[1]), 15)
finaldate = Date(year(t0[end]), month(t0[end]), 16)
startdate = Date(year(t0[1]), month(t0[1]), mday)
finaldate = Date(year(t0[end]), month(t0[end]), mday+1)
t = startdate:Month(1):finaldate
tranges = temporalrange(t0, Dates.month)
return timegroup(A, f, t, tranges, "monthly")
Expand Down
16 changes: 7 additions & 9 deletions src/tsa/decomposition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ periodic parts of `A`, with frequencies given in `fs`, and residual contains wha
`fs` is measured in 1/year. This function works even for non-equispaced time axis (e.g.
monthly averages) and uses LPVSpectral.jl and SignalDecomposition.jl.
"""
seasonal_decomposition(A::AbDimArray, b) = seasonal_decomposition(dims(A, Time), A, b)

function seasonal_decomposition(t, A::AbDimArray, fs::Vector)
function seasonal_decomposition(A::AbDimArray, fs::Vector)
@assert hasdim(A, Time)
E = _numbertype(T)
E = _numbertype(eltype(A))
method = Sinusoidal(E.(fs ./ DAYS_IN_ORBIT))
seasonal = DimensionalData.basetypeof(A)(copy(Array(A)), dims(A); name = A.name*"seasonal")
residual = DimensionalData.basetypeof(A)(copy(Array(A)), dims(A); name = A.name*"residual")
seasonal = DimensionalData.basetypeof(A)(copy(Array(A)), dims(A))
residual = DimensionalData.basetypeof(A)(copy(Array(A)), dims(A))

# TODO: Fix this to use "real time"
truetime = Float32.(cumsum(daysinmonth.(t)))
for i in otheridxs(T, Time)
t = dims(A, Time).val
truetime = time_in_days(t, E)
for i in otheridxs(A, Time)
y = Array(A[i...])
sea, res = SignalDecomposition.decompose(truetime, y, method)
seasonal[i...] .= sea
Expand Down

0 comments on commit e1cb93e

Please sign in to comment.