tSet NetCDF as optional dependency - Granular.jl - Julia package for granular d… | |
git clone git://src.adamsgaard.dk/Granular.jl | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 05ebf4c41035547007acc8e019ed0f8cbf3d3723 | |
parent 2ab3e5cc94c45a4baff6d1e5ca769c3eeebe0309 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Tue, 27 Feb 2018 21:56:16 -0500 | |
Set NetCDF as optional dependency | |
Diffstat: | |
M .travis.yml | 2 +- | |
M REQUIRE | 1 - | |
M src/io.jl | 3 ++- | |
M src/ocean.jl | 132 +++++++++++++++++++----------… | |
4 files changed, 86 insertions(+), 52 deletions(-) | |
--- | |
diff --git a/.travis.yml b/.travis.yml | |
t@@ -27,7 +27,7 @@ matrix: | |
- julia: nightly | |
before_script: | |
- - julia -e 'info("Preparing Python"); ENV["PYTHON"]=""; Pkg.add("PyCall"); P… | |
+ - julia -e 'info("Preparing Python"); ENV["PYTHON"]=""; Pkg.add("PyCall"); P… | |
script: | |
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi | |
diff --git a/REQUIRE b/REQUIRE | |
t@@ -1,5 +1,4 @@ | |
julia 0.6 | |
WriteVTK | |
-NetCDF | |
Documenter | |
Compat 0.42.0 | |
diff --git a/src/io.jl b/src/io.jl | |
t@@ -1,10 +1,11 @@ | |
import WriteVTK | |
-import NetCDF | |
+ | |
hasJLD = false | |
if typeof(Pkg.installed("JLD")) == VersionNumber | |
import JLD | |
hasJLD = true | |
end | |
+ | |
import Compat | |
using Compat.DelimitedFiles | |
using Compat.Dates | |
diff --git a/src/ocean.jl b/src/ocean.jl | |
t@@ -1,3 +1,16 @@ | |
+hasNetCDF = false | |
+if typeof(Pkg.installed("NetCDF")) == VersionNumber | |
+ import NetCDF | |
+ hasNetCDF = true | |
+else | |
+ if !hasNetCDF | |
+ warn("Package NetCDF not found. " * | |
+ "Ocean/atmosphere grid read not supported. " * | |
+ "Please install NetCDF and its " * | |
+ "requirements with `Pkg.add(\"NetCDF\")`.") | |
+ end | |
+end | |
+ | |
using Compat.Test | |
export createEmptyOcean | |
t@@ -41,39 +54,46 @@ structure. | |
function readOceanNetCDF(velocity_file::String, grid_file::String; | |
regular_grid::Bool=false) | |
- time, u, v, h, e, zl, zi = readOceanStateNetCDF(velocity_file) | |
- xh, yh, xq, yq = readOceanGridNetCDF(grid_file) | |
+ if !hasNetCDF | |
+ warn("Package NetCDF not found. Ocean/atmosphere grid read not support… | |
+ "Please install NetCDF and its " * | |
+ "requirements with `Pkg.add(\"NetCDF\")`.") | |
+ else | |
- if size(u[:,:,1,1]) != size(xq) || size(v[:,:,1,1]) != size(xq) || | |
- size(xq) != size(yq) | |
- error("size mismatch between velocities and grid | |
- (u: $(size(u[:,:,1,1])), v: $(size(v[:,:,1,1])), | |
- xq: $(size(xq)), yq: $(size(yq)))") | |
- end | |
+ time, u, v, h, e, zl, zi = readOceanStateNetCDF(velocity_file) | |
+ xh, yh, xq, yq = readOceanGridNetCDF(grid_file) | |
- ocean = Ocean([grid_file, velocity_file], | |
+ if size(u[:,:,1,1]) != size(xq) || size(v[:,:,1,1]) != size(xq) || | |
+ size(xq) != size(yq) | |
+ error("size mismatch between velocities and grid | |
+ (u: $(size(u[:,:,1,1])), v: $(size(v[:,:,1,1])), | |
+ xq: $(size(xq)), yq: $(size(yq)))") | |
+ end | |
+ | |
+ ocean = Ocean([grid_file, velocity_file], | |
- time, | |
+ time, | |
- xq, | |
- yq, | |
- xh, | |
- yh, | |
+ xq, | |
+ yq, | |
+ xh, | |
+ yh, | |
- zl, | |
- zi, | |
+ zl, | |
+ zi, | |
- u, | |
- v, | |
- h, | |
- e, | |
- Array{Array{Int, 1}}(size(xh, 1), size(xh, 2)), | |
- zeros(size(xh)), | |
- 1, 1, 1, 1, | |
+ u, | |
+ v, | |
+ h, | |
+ e, | |
+ Array{Array{Int, 1}}(size(xh, 1), size(xh, 2)), | |
+ zeros(size(xh)), | |
+ 1, 1, 1, 1, | |
- false, [0.,0.,0.], [1.,1.,1.], [1,1,1], [1.,1.,1.] | |
- ) | |
- return ocean | |
+ false, [0.,0.,0.], [1.,1.,1.], [1,1,1], [1.,1.,1.] | |
+ ) | |
+ return ocean | |
+ end | |
end | |
export readOceanStateNetCDF | |
t@@ -97,23 +117,30 @@ layer thicknesses, interface heights, and vertical coordi… | |
""" | |
function readOceanStateNetCDF(filename::String) | |
- if !isfile(filename) | |
- error("$(filename) could not be opened") | |
- end | |
+ if !hasNetCDF | |
+ warn("Package NetCDF not found. Ocean/atmosphere grid read not support… | |
+ "Please install NetCDF and its " * | |
+ "requirements with `Pkg.add(\"NetCDF\")`.") | |
+ else | |
- u_staggered = convert(Array{Float64, 4}, NetCDF.ncread(filename, "u")) | |
- v_staggered = convert(Array{Float64, 4}, NetCDF.ncread(filename, "v")) | |
- u, v = interpolateOceanVelocitiesToCorners(u_staggered, v_staggered) | |
+ if !isfile(filename) | |
+ error("$(filename) could not be opened") | |
+ end | |
+ | |
+ u_staggered = convert(Array{Float64, 4}, NetCDF.ncread(filename, "u")) | |
+ v_staggered = convert(Array{Float64, 4}, NetCDF.ncread(filename, "v")) | |
+ u, v = interpolateOceanVelocitiesToCorners(u_staggered, v_staggered) | |
- time = convert(Vector{Float64}, | |
- NetCDF.ncread(filename, "time") .* 24. * 60. * 60.) | |
- h = convert(Array{Float64, 4}, NetCDF.ncread(filename, "h")) | |
- e = convert(Array{Float64, 4}, NetCDF.ncread(filename, "e")) | |
+ time = convert(Vector{Float64}, | |
+ NetCDF.ncread(filename, "time") .* 24. * 60. * 60.) | |
+ h = convert(Array{Float64, 4}, NetCDF.ncread(filename, "h")) | |
+ e = convert(Array{Float64, 4}, NetCDF.ncread(filename, "e")) | |
- zl = convert(Vector{Float64}, NetCDF.ncread(filename, "zl")) | |
- zi = convert(Vector{Float64}, NetCDF.ncread(filename, "zi")) | |
+ zl = convert(Vector{Float64}, NetCDF.ncread(filename, "zl")) | |
+ zi = convert(Vector{Float64}, NetCDF.ncread(filename, "zi")) | |
- return time, u, v, h, e, zl, zi | |
+ return time, u, v, h, e, zl, zi | |
+ end | |
end | |
export readOceanGridNetCDF | |
t@@ -130,19 +157,26 @@ located in the simulation `INPUT/` subdirectory. | |
""" | |
function readOceanGridNetCDF(filename::String) | |
- if !isfile(filename) | |
- error("$(filename) could not be opened") | |
- end | |
- x = convert(Array{Float64, 2}, NetCDF.ncread(filename, "x")) | |
- y = convert(Array{Float64, 2}, NetCDF.ncread(filename, "y")) | |
+ if !hasNetCDF | |
+ warn("Package NetCDF not found. Ocean/atmosphere grid read not support… | |
+ "Please install NetCDF and its " * | |
+ "requirements with `Pkg.add(\"NetCDF\")`.") | |
+ else | |
- xh = x[2:2:end, 2:2:end] | |
- yh = y[2:2:end, 2:2:end] | |
+ if !isfile(filename) | |
+ error("$(filename) could not be opened") | |
+ end | |
+ x = convert(Array{Float64, 2}, NetCDF.ncread(filename, "x")) | |
+ y = convert(Array{Float64, 2}, NetCDF.ncread(filename, "y")) | |
+ | |
+ xh = x[2:2:end, 2:2:end] | |
+ yh = y[2:2:end, 2:2:end] | |
- xq = x[1:2:end, 1:2:end] | |
- yq = y[1:2:end, 1:2:end] | |
+ xq = x[1:2:end, 1:2:end] | |
+ yq = y[1:2:end, 1:2:end] | |
- return xh, yh, xq, yq | |
+ return xh, yh, xq, yq | |
+ end | |
end | |
export interpolateOceanVelocitiesToCorners |