Skip to content

Commit

Permalink
add tests for utilize
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Jan 21, 2024
1 parent 4fc86ac commit 9af5e47
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/utilize/nc_combine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function nc_get_value(fs, band=nothing; verbose=false)
for f = fs
verbose && (println(basename(f)))
nc_open(f) do ds
_data = ds[band].var[:]
_data = ds[band].var |> collect
_dates = nc_date(ds)
push!(data, _data)
push!(dates, _dates)
Expand All @@ -23,7 +23,11 @@ function nc_get_value(fs, band=nothing; verbose=false)
selectdim(data, ndim, inds), dates[inds]
end

"""
nc_combine(fs, fout; compress=0)
!会自动跳过重复的日期
"""
function nc_combine(fs, fout; compress=0)
f = fs[1]
nc = nc_open(f)
Expand All @@ -34,13 +38,16 @@ function nc_combine(fs, fout; compress=0)
@time vals, dates = nc_get_value(fs, band)
# times = nc_get_value(fs, "time")

att = nc["time"].attrib
times = CFTime.timeencode(dates, att["units"], att["calendar"])

if eltype(dates) <: Real
times = dates
else
att = nc["time"].attrib
times = CFTime.timeencode(dates, att["units"], att["calendar"])
end
dims = ncvar_dim(nc)
dims["time"] = NcDim("time", times, dims["time"].atts)
# 这里会引起错误
dims["time"] = NcDim("time", dates, dims["time"].atts)

# 这里会引起错误
printstyled("Writing data...\n")
@time nc_write(fout, band, vals, dims, Dict(v.attrib);
compress, global_attrib=Dict(nc.attrib))
Expand Down
10 changes: 7 additions & 3 deletions src/utilize/nc_subset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ function nc_subset(f, range::Vector, fout=nothing;
## 截取数据
in_plev(plev, plevs) = indexin(round.(Int, plev), plevs*100) .!== nothing

v = @select(nc[band], $lonr[1] <= lon <= $lonr[2] && $latr[1] <= lat <= $latr[2])
if haskey(nc, "lon") && haskey(nc, "lat")
v = @select(nc[band], $lonr[1] <= lon <= $lonr[2] && $latr[1] <= lat <= $latr[2])
elseif haskey(nc, "x") && haskey(nc, "y")
v = @select(nc[band], $lonr[1] <= x <= $lonr[2] && $latr[1] <= y <= $latr[2])
end

(ilon, ilat, _) = parentindices(v)
dims["lon"] = dims["lon"][ilon]
dims["lat"] = dims["lat"][ilat]
dims[1] = dims[1][ilon]
dims[2] = dims[2][ilat]

if plevs !== nothing && ndims(v) == 4
v = @select(v, in_plev(plev, $plevs))
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ proj_path(f) = dirname(dirname(@__FILE__)) * "/" * f
# println(pwd())

# cd(dirname(@__FILE__)) do
include("test-utilize.jl")
include("test-ncatt.jl")
include("test-bilinear.jl")
include("test-MFDataset.jl")
Expand Down
15 changes: 15 additions & 0 deletions test/test-utilize.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testset "nc_combine" begin
fs = dir(proj_path("data/nc/"))
fout = "combine.nc"
@test_nowarn nc_combine(fs, fout)
isfile(fout) && rm(fout)
end


@testset "nc_subset" begin
f = dir(proj_path("data/nc/"))[1]
fout = "subset.nc"
@test_nowarn nc_subset(f, [100, 120, 20, 40], fout)
isfile(fout) && rm(fout)
end

0 comments on commit 9af5e47

Please sign in to comment.