Skip to content

Commit

Permalink
add cdo
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Dec 3, 2023
1 parent 8a8a9e4 commit 9a74f56
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Terra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ include("MFDataset/MFDataset.jl")
include("apply.jl")
include("tools.jl")

include("main_cdo.jl")

export rast, brick,
rast_apply,
Expand Down
41 changes: 41 additions & 0 deletions src/main_cdo.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function cdo_grid(ra::AbstractRaster; fout="grid.txt", kw...)
x, y = st_dims(ra)
cdo_grid(x, y; fout, kw...)
end

function cdo_grid(x::AbstractVector, y::AbstractVector;
fout="grid.txt", verbose=true)

grid = """
gridtype = lonlat
xsize = $(length(x))
ysize = $(length(y))
xfirst = $(minimum(x))
xinc = 1
yfirst = $(minimum(y))
yinc = 1"""

verbose && println(grid)
writelines([grid], fout)
fout
end

function cdo_grid(range, cellsize, mid::Bool; fout="grid.txt", kw...)
delta = mid ? cellsize / 2 : 0
x = range[1]+delta:cellsize:range[2]
y = range[3]+delta:cellsize:range[4]
cdo_grid(x, y; fout, kw...)
end

function cdo_bilinear(fin, fout, fgrid;
options=`-f nc4 -z zip_1`, cdo="/opt/miniconda3/bin/cdo", verbose=false)
wsl = (is_wsl() || is_windows()) ? "wsl" : ""
# run(`$wsl $cdo -v`)
cmd = `$wsl $cdo $options remapbil,$fgrid $fin $fout`
verbose && println(cmd)
run(cmd)
nothing
end


export cdo_grid, cdo_bilinear
13 changes: 13 additions & 0 deletions src/tools_Ipaper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ fliplr(x::AbstractArray{T,2}) where {T<:Real} = x[:, end:-1:1]
fliplr(x::AbstractArray{T,3}) where {T<:Real} = x[:, end:-1:1, :]

# add a resample function at here

is_wsl() = Sys.islinux() && isfile("/mnt/c/Windows/System32/cmd.exe")
is_windows() = Sys.iswindows()
is_linux() = Sys.islinux()

function writelines(x::AbstractVector{<:AbstractString}, f; mode="w", eof="\n")
fid = open(f, mode)
@inbounds for _x in x
write(fid, _x)
write(fid, eof)
end
close(fid)
end

0 comments on commit 9a74f56

Please sign in to comment.