|
1 | | -function check_landmask_path(lmpath::String)::Nothing |
2 | | - name = basename(lmpath) |
3 | | - input = dirname(lmpath) |
4 | | - !isfile(lmpath) && error( |
5 | | - "`$(name)` not found in $input. Please ensure a coastline image file named `$name` exists in $input.", |
6 | | - ) |
7 | | - return nothing |
8 | | -end |
9 | | - |
10 | | -""" |
11 | | - landmask(; input, output) |
12 | | -
|
13 | | -Given an input directory with a landmask file and possibly truecolor images, create a land/soft ice mask. The resulting images are saved to the snakemake output directory. |
14 | | -
|
15 | | -# Arguments |
16 | | -- `input`: path to image dir containing truecolor and landmask source images |
17 | | -- `output`: path to output dir where land-masked truecolor images and the generated binary land mask are saved |
18 | | -
|
19 | | -""" |
20 | | -function landmask(; input::String, output::String) |
21 | | - landmask_fname = "landmask.tiff" |
22 | | - @info "Looking for $landmask_fname in $input" |
23 | | - |
24 | | - lmpath = joinpath(input, landmask_fname) |
25 | | - check_landmask_path(lmpath) |
26 | | - @info "$landmask_fname found in $input. Creating landmask..." |
27 | | - |
28 | | - img = load(lmpath) |
29 | | - mkpath(output) |
30 | | - out = @persist create_landmask(img) joinpath(output, "generated_landmask.png") |
31 | | - @info "Landmask created succesfully." |
32 | | - return out |
33 | | -end |
| 1 | +function check_landmask_path(lmpath::String)::Nothing |
| 2 | + name = basename(lmpath) |
| 3 | + input = dirname(lmpath) |
| 4 | + !isfile(lmpath) && error( |
| 5 | + "`$(name)` not found in $input. Please ensure a coastline image file named `$name` exists in $input.", |
| 6 | + ) |
| 7 | + return nothing |
| 8 | +end |
| 9 | + |
| 10 | +""" |
| 11 | + landmask(; input, output) |
| 12 | +
|
| 13 | +Given an input directory with a landmask file and possibly truecolor images, create a land/soft ice mask. The resulting images are saved to the snakemake output directory. Returns the landmkask object. |
| 14 | +
|
| 15 | +# Arguments |
| 16 | +- `input`: path to image dir containing truecolor and landmask source images |
| 17 | +- `output`: path to output dir where land-masked truecolor images and the generated binary land mask are saved |
| 18 | +
|
| 19 | +""" |
| 20 | +function landmask(; input::String, output::String) |
| 21 | + landmask_fname = "landmask.tiff" |
| 22 | + @info "Looking for $landmask_fname in $input" |
| 23 | + |
| 24 | + lmpath = joinpath(input, landmask_fname) |
| 25 | + check_landmask_path(lmpath) |
| 26 | + @info "$landmask_fname found in $input. Creating landmask..." |
| 27 | + |
| 28 | + img = load(lmpath) |
| 29 | + mkpath(output) |
| 30 | + out = @persist create_landmask(img) joinpath(output, "generated_landmask.png") |
| 31 | + @info "Landmask created succesfully." |
| 32 | + return out |
| 33 | +end |
| 34 | + |
| 35 | +""" |
| 36 | + cache_vector(type::Type, numel::Int64, size::Tuple{Int64, Int64})::Vector{type} |
| 37 | +
|
| 38 | +Build a vector of types `type` with `numel` elements of size `size`. |
| 39 | +
|
| 40 | +Example |
| 41 | +
|
| 42 | +```jldoctest |
| 43 | +julia> cache_vector(Matrix{Float64}, 3, (2, 2)) |
| 44 | +3-element Vector{Matrix{Float64}}: |
| 45 | + [0.0 6.9525705991269e-310; 6.9525705991269e-310 0.0] |
| 46 | + [0.0 6.9525705991269e-310; 6.9525705991269e-310 0.0] |
| 47 | + [0.0 6.95257028858726e-310; 6.95257029000147e-310 0.0] |
| 48 | +``` |
| 49 | +""" |
| 50 | +function cache_vector(type::Type, numel::Int64, size::Tuple{Int64,Int64})::Vector{type} |
| 51 | + return [type(undef, size) for _ in 1:numel] |
| 52 | +end |
| 53 | + |
| 54 | +""" |
| 55 | + load_imgs(; input::String, image_type::String) |
| 56 | +
|
| 57 | +Load all images of type `image_type` (either `"truecolor"` or `"reflectance"`) in `input` into a vector. |
| 58 | +""" |
| 59 | +function load_imgs(; input::String, image_type::Union{Symbol,String}) |
| 60 | + return [ |
| 61 | + float64.(load(joinpath(input, f))) for |
| 62 | + f in readdir(input) if contains(f, string(image_type)) |
| 63 | + ] |
| 64 | +end |
| 65 | + |
| 66 | +function load_truecolor_imgs(; input::String) |
| 67 | + return load_imgs(; input=input, image_type="truecolor") |
| 68 | +end |
| 69 | + |
| 70 | +function load_reflectance_imgs(; input::String) |
| 71 | + return load_imgs(; input=input, image_type="reflectance") |
| 72 | +end |
| 73 | + |
| 74 | +function cloudmask(; input::String, output::String)::Vector{BitMatrix} |
| 75 | + # find reflectance imgs in input dir |
| 76 | + ref = [img for img in readdir(input) if contains(img, "reflectance")] # ref is sorted |
| 77 | + total_ref = length(ref) |
| 78 | + @info "Found $(total_ref) reflectance images in $input. |
| 79 | + Cloudmasking false color images..." |
| 80 | + |
| 81 | + # Preallocate container for the cloudmasks |
| 82 | + ref_img = IceFloeTracker.float64.(IceFloeTracker.load(joinpath(input, ref[1]))) # read in the first one to retrieve size |
| 83 | + sz = size(ref_img) |
| 84 | + cloudmasks = [BitMatrix(undef, sz) for _ in 1:total_ref] |
| 85 | + |
| 86 | + # Do the first one because it's loaded already |
| 87 | + cloudmasks[1] = IceFloeTracker.create_cloudmask(ref_img) |
| 88 | + # and now the rest |
| 89 | + for i in 2:total_ref |
| 90 | + img = IceFloeTracker.float64.(IceFloeTracker.load(joinpath(input, ref[i]))) |
| 91 | + cloudmasks[i] = IceFloeTracker.create_cloudmask(img) |
| 92 | + end |
| 93 | + return cloudmasks |
| 94 | +end |
0 commit comments