Skip to content

Commit

Permalink
Merge pull request #63 from GenericMappingTools/fixes-band-names
Browse files Browse the repository at this point in the history
Fix things that broke after the default band names ("Band1" instead of "Band 1"
  • Loading branch information
joa-quim authored Dec 19, 2023
2 parents e3e61da + 231bc4a commit 7bce1b0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RemoteS"
uuid = "26020ff4-e92a-488e-a723-09c28e89e04a"
authors = ["Joaquim Luis <jmfluis@gmail.com>"]
version = "1.1.0"
version = "1.1.1"

[deps]
GMT = "5752ebe1-31b9-557e-87aa-f909b540aa54"
Expand All @@ -21,7 +21,7 @@ test = ["Test"]

[compat]
julia = "1.6"
GMT = "1"
GMT = "1.9"
PrecompileTools = "1.0"
SatelliteToolboxTle = "1"
SatelliteToolboxPropagators = "0.3"
Expand Down
12 changes: 6 additions & 6 deletions src/spectral_indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const generic_docs = "
- The second form is more versatile but also more complex to describe.
- `cube`: Is the file name of a 'cube', a multi-layered file normally created with the [`cutcube`](@ref) function.
If this file was created with band descriptions one can use the `bands` or the `bandnames` options.
- `bands`: _cubes_ created with [`cutcube`](@ref) assign descriptions starting with \"Band 1 ...\" an so on
the other bands. So when `bands` is used we search for bands named \"Band band[k]\", where band[k] loops
- `bands`: _cubes_ created with [`cutcube`](@ref) assign descriptions starting with \"Band1 ...\" an so on
the other bands. So when `bands` is used we search for bands named \"Band'band[k]'\", where band[k] loops
over all elements of the `bands` vector. WARNING: the elements order in the vector must be sorted in increasing
wavelength numbers, _i.e._ like the example for the first form.
- `layers`: Use this option when you are certain of the bands order in the cube or the it doesn't have a bands
Expand Down Expand Up @@ -130,7 +130,7 @@ $(generic_docs)
mndwi(green, swir2; kw...) = sp_indices(swir2, green; index="MNDWI", kw...)
mndwi(cube::GMT.GMTimage{UInt16, 3}, bnds; kw...) = sp_indices(cube, find_layers(cube, bnds, 2); index="MNDWI", kw...)
mndwi(cube::String; bands::Vector{Int}=Int[], layers::Vector{Int}=Int[], bandnames::Vector{String}=String[], kw...) =
helper_si_method(cube, "MNDWI"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["green", "swir 2"], kw...)
helper_si_method(cube, "MNDWI"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["green", "swir2"], kw...)

# ----------------------------------------------------------------------------------------------------------
"""
Expand Down Expand Up @@ -238,7 +238,7 @@ $(generic_docs)
ndwi2(nir, swir2; kw...) = sp_indices(swir2, nir; index="NDWI2", kw...)
ndwi2(cube::GMT.GMTimage{UInt16, 3}, bnds; kw...) = sp_indices(cube, find_layers(cube, bnds, 2); index="NDWI2", kw...)
ndwi2(cube::String; bands::Vector{Int}=Int[], layers::Vector{Int}=Int[], bandnames::Vector{String}=String[], kw...) =
helper_si_method(cube, "NDWI2"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["nir", "swir 2"], kw...)
helper_si_method(cube, "NDWI2"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["nir", "swir2"], kw...)

# ----------------------------------------------------------------------------------------------------------
"""
Expand Down Expand Up @@ -273,7 +273,7 @@ SATVI = ((swir1 - red) / (swir1 + red + L)) * (1.0 + L) - (swir2 / 2.0)
satvi(red, swir2, swir3; kw...) = sp_indices(red, swir2, swir3; index="SATVI", kw...)
satvi(cube::GMT.GMTimage{UInt16, 3}, bnds; kw...) = sp_indices(cube, find_layers(cube, bnds, 3); index="SATVI", kw...)
satvi(cube::String; bands::Vector{Int}=Int[], layers::Vector{Int}=Int[], bandnames::Vector{String}=String[], kw...) =
helper_si_method(cube, "SATVI"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["red", "swir 1", "swir 2"], kw...)
helper_si_method(cube, "SATVI"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["red", "swir1", "swir2"], kw...)

# ----------------------------------------------------------------------------------------------------------
"""
Expand Down Expand Up @@ -309,7 +309,7 @@ $(generic_docs)
slavi(red, nir, swir2; kw...) = sp_indices(red, nir, swir2; index="SLAVI", kw...)
slavi(cube::GMT.GMTimage{UInt16, 3}, bnds; kw...) = sp_indices(cube, find_layers(cube, bnds, 3); index="SLAVI", kw...)
slavi(cube::String; bands::Vector{Int}=Int[], layers::Vector{Int}=Int[], bandnames::Vector{String}=String[], kw...) =
helper_si_method(cube, "SLAVI"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["red", "nir", "swir 2"], kw...)
helper_si_method(cube, "SLAVI"; bands=bands, layers=layers, bandnames=bandnames, defbandnames=["red", "nir", "swir2"], kw...)

# ----------------------------------------------------------------------------------------------------------
function helper_sp_indices(kwargs...)
Expand Down
29 changes: 20 additions & 9 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ we search for a made up `bandnames=["red", "green", "blue"]`.
Return an auto-stretched UInt8 RGB GMTimage OR a GMTimage{UInt16,3} if the `raw` option is set to `true`.
Irgb = truecolor(cube::GMTgrid, [bands|layers::Vector{Int}], [bandnames::Vector{String}], [type=UInt8])
Make an RGB composition of 3 bands from the `cube` file holding a Float32 multi-layered array.
The band selection can be made with `bands` vector, case in which we will search for bands named "Band band[k]"
or where the bands description contain the contents of `bandnames`. If none of `bands` or `bandnames` is used
we search for a made up `bandnames=["red", "green", "blue"]`.
By default we scale the bands to 0-255. Use `type=UInt16` to scale the bands to 0-65535`. Note that this
will matter only for the guessing of the good limits to perform the histogram stretching.
### Example:
Make an RGB composite from data in the cube file "LC08__cube.tiff"
```julia
Expand Down Expand Up @@ -102,7 +113,7 @@ function truecolor(bndR, bndG, bndB)
end
Io = mat2img(img, I);
Io.layout = (isa(bndR, GMT.GMTimage)) ? "T" * bndR.layout[2] * "Ba" : "TRBa" # This is shitty fragile
(isa(bndR, GMT.GMTimage) && startswith(bndR.layout, "BC")) && (Io.layout = "BCBa") # Hoorible patch that needs to know why.
(isa(bndR, GMT.GMTimage) && startswith(bndR.layout, "BC")) && (Io.layout = "BCBa") # Horrible patch that needs to know why.
Io
end

Expand All @@ -121,14 +132,14 @@ end

truecolor(cube::GMT.GMTgrid{Float32, 3}, layers::Vector{Int}) = truecolor(cube, layers=layers)
function truecolor(cube::GMT.GMTgrid{Float32, 3}; bands::Vector{Int}=Int[], layers::Vector{Int}=Int[],
bandnames::Vector{String}=String[])
bandnames::Vector{String}=String[], type::DataType=UInt8)
(isempty(bands) && isempty(bandnames) && isempty(layers)) && (bandnames = ["red", "green", "blue"])
isempty(layers) && (layers = find_layers(cube, bandnames, bands))
(length(layers) != 3) && error("For an RGB composition 'bands' must be a 3 elements array and not $(length(layers))")
img = Array{UInt8, 3}(undef, size(cube,1), size(cube,2), 3)
img[:,:,1] = rescale(@view(cube[:,:,layers[1]]), stretch=true, type=UInt8)
img[:,:,2] = rescale(@view(cube[:,:,layers[2]]), stretch=true, type=UInt8)
img[:,:,3] = rescale(@view(cube[:,:,layers[3]]), stretch=true, type=UInt8)
img = Array{type, 3}(undef, size(cube,1), size(cube,2), 3)
img[:,:,1] = rescale(@view(cube[:,:,layers[1]]), stretch=true, type=type)
img[:,:,2] = rescale(@view(cube[:,:,layers[2]]), stretch=true, type=type)
img[:,:,3] = rescale(@view(cube[:,:,layers[3]]), stretch=true, type=type)
Io = mat2img(img, cube); Io.layout = "TRBa"
Io
end
Expand Down Expand Up @@ -223,7 +234,7 @@ function find_layers(fname::String; bands::Vector{Int}=Int[], layers::Vector{Int
(all(desc .== "")) && error("This cube file has no band descriptions so cannot use the 'band' or 'bandnames' options.")
(alllayers) && return collect(1:nbands), desc # OK, just return them ALL

(!isempty(bands)) && (bandnames = ["Band $(bands[k])" for k = 1:length(bands)]) # Create a bandnames vector
(!isempty(bands)) && (bandnames = ["Band$(bands[k])" for k = 1:length(bands)]) # Create a bandnames vector
_layers = helper_find_layers(lowercase.(desc), bandnames)
return _layers, desc
end
Expand Down Expand Up @@ -484,15 +495,15 @@ function parse_lsat8_file(fname::String; band::Int=0, layer::Int=0, mtl::String=

if (nbands > 1)
if (0 < layer < 12) # Try to find which Band is at layer 'layer'
((_band = tryparse(Int, split(desc[layer])[2])) === nothing) && @warn("Failed to find Band with description in layer $layout")
((_band = tryparse(Int, split(desc[layer])[1][5:end])) === nothing) && @warn("Failed to find Band with description in layer $layer")
(_band !== nothing) && (band = _band)
end
(band < 1) && error("The `band` option must contain the wished Landsat 8 band number. Use `band=N` to set it.")
(band > 11) && throw(ArgumentError("Bad Landsat 8 band number $band"))
MTL = ((ind = findfirst(startswith.(meta, "MTL=GROUP ="))) !== nothing) ? meta[ind][5:end] :
error("Data in a cube (> one layer) must contain the MTL info in Metadata and this one does not.")

((band_layer = findfirst(startswith.(desc, "Band $band"))) === nothing) && error("Band $band not found in this cube")
((band_layer = findfirst(startswith.(desc, "Band$band"))) === nothing) && error("Band $band not found in this cube")
pars = parse_mtl(split(MTL, "\n"), band)
else
pars = read_mtl(fname, mtl) # No 'band' here because it's suposed to be findable in 'fname'
Expand Down
Binary file modified test/LC08_cube.tiff
Binary file not shown.
10 changes: 5 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ truecolor(mat2img(rand(UInt16, 16, 16), noconv=1), mat2img(rand(UInt16, 16, 16),
@test RemoteS.assign_description(["AC08_B2.TIF", "AC08_B5.TIF"], String[]) == ["AC08_B2", "AC08_B5"]
@test RemoteS.assign_description(["AC08_B2.TIF", "AC08_B5.TIF"], ["B2", "B5"]) == ["B2", "B5"]

@test reportbands(mat2img(rand(UInt16, 4,4,3), names=["Band 1", "Band 2", "Band 3"]), 3)[1] == "Band 3"
@test reportbands(mat2img(rand(UInt16, 4,4,3), names=["Band 1", "Band 2", "Band 3"]), layers=[1,3]) == ["Band 1", "Band 3"]
@test reportbands(mat2img(rand(UInt16, 4,4,3), names=["Band1", "Band2", "Band3"]), 3)[1] == "Band3"
@test reportbands(mat2img(rand(UInt16, 4,4,3), names=["Band1", "Band2", "Band3"]), layers=[1,3]) == ["Band1", "Band3"]

#sat_tracks(position=true);
#sat_tracks(position=true, tle=["1 27424U 02022A 21245.83760660 .00000135 00000-0 39999-4 0 9997"; "2 27424 98.2123 186.0654 0002229 67.6025 313.3829 14.57107527 28342"]);
Expand All @@ -31,7 +31,7 @@ truecolor("LC08_cube.tiff"); println(" truecolor 1")
truecolor(cube, [3,2,1]); println(" truecolor 2")
truecolor(cube, layers=[3,2,1]); println(" truecolor 3")
println(" reportbands 1")
@test reportbands("LC08_cube.tiff", 2)[1] == "Band 3 - Green [0.53-0.59]"
@test reportbands("LC08_cube.tiff", 2)[1] == "Band3 - Green [0.53-0.59]"
println(" reportbands 2")
reportbands(cube);
println(" subcube")
Expand All @@ -46,8 +46,8 @@ R = subcube("LC08_cube.tiff", bandnames=["red"]); println(" subcube R")
G = subcube("LC08_cube.tiff", bandnames=["green"]); println(" subcube G")
B = subcube("LC08_cube.tiff", bandnames=["blue"]); println(" subcube B")
NIR = subcube("LC08_cube.tiff", bandnames=["nir"]); println(" subcube NIR")
SW1 = subcube("LC08_cube.tiff", bandnames=["swir 1"]); println(" subcube SW1")
SW2 = subcube("LC08_cube.tiff", bandnames=["swir 2"]); println(" subcube SW2")
SW1 = subcube("LC08_cube.tiff", bandnames=["swir1"]); println(" subcube SW1")
SW2 = subcube("LC08_cube.tiff", bandnames=["swir2"]); println(" subcube SW2")
G1 = evi("LC08_cube.tiff"); println(" evi 1")
G2 = evi(B, R, NIR); println(" evi 2")
G3 = evi(cube,[1,3,4]); println(" evi 3")
Expand Down

0 comments on commit 7bce1b0

Please sign in to comment.