Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add makeh5files_single CLI command to track floes in individual files #192

Draft
wants to merge 4 commits into
base: jghrefactor/A2e-add-single-file-versions-of-cli-tracker
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions IFTPipeline.jl/src/cli.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,45 @@ function mkclimakeh5!(settings)
return nothing
end

function mkclimakeh5_single!(settings)
@add_arg_table! settings["makeh5files_single"] begin
"--iftversion"
help = "Version number of the IceFloeTracker.jl package"
required = false
arg_type = String

"--passtime"
help = "Satellite pass time"
required = true
arg_type = DateTime

"--truecolor"
help = "Path to truecolor image"
required = true
arg_type = String

"--falsecolor"
help = "Path to falsecolor image"
required = true
arg_type = String

"--labeled"
help = "Path to labeled image"
required = true
arg_type = String

"--props"
help = "Path to extracted features (csv)"
required = true
arg_type = String

"--output", "-o"
help = "Output file"
required = true
end
return nothing
end

function mkclitrack!(settings)
add_arg_group!(settings["track"], "arguments")
@add_arg_table! settings["track"] begin
Expand Down Expand Up @@ -420,6 +459,7 @@ function mkcli!(settings, common_args)
"extractfeatures" => mkcliextract!,
"extractfeatures_single" => mkcliextract_single!,
"makeh5files" => mkclimakeh5!,
"makeh5files_single" => mkclimakeh5_single!,
"track" => mkclitrack!,
"track_single" => mkclitrack_single!,
)
Expand Down Expand Up @@ -465,6 +505,10 @@ function main()
help = "Make HDF5 files from extracted floe features"
action = :command

"makeh5files_single"
help = "Make HDF5 files from extracted floe features"
action = :command

"track"
help = "Pair ice floes in day k with ice floes in day k+1"
action = :command
Expand Down
50 changes: 50 additions & 0 deletions IFTPipeline.jl/src/h5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,53 @@ function makeh5files(; pathtosampleimg::String, resdir::String, iftversion=IceFl
end
return nothing
end


function makeh5files_single(; passtime::DateTime, iftversion::Union{String, Nothing}=nothing, truecolor::String, falsecolor::String, labeled::String, props::String, output::String)
latlondata = getlatlon(truecolor)
ptsunix = Int64(Dates.datetime2unix(passtime))
labeled_ = Integer.(rawview(channelview((FileIO.load(labeled)))))

if isnothing(iftversion)
iftversion = string(IceFloeTracker.IFTVERSION)
end

props_ = DataFrame(CSV.File(props))
colstodrop = [:row_centroid, :col_centroid, :min_row, :min_col, :max_row, :max_col]
converttounits!(props_, latlondata, colstodrop)
@info props_

h5open(output, "w") do file
# Add top-level attributes
attrs(file)["fname_falsecolor"] = falsecolor
attrs(file)["fname_truecolor"] = truecolor
attrs(file)["iftversion"] = iftversion
attrs(file)["crs"] = latlondata["crs"]
attrs(file)["reference"] = "https://doi.org/10.1016/j.rse.2019.111406"
attrs(file)["contact"] = "[email protected]"

g = create_group(file, "index")
g["time"] = ptsunix
g["x"] = latlondata["X"]
g["y"] = latlondata["Y"]

g = create_group(file, "floe_properties")
write_dataset(g, "properties", [copy(row) for row in eachrow(props_)]) # `copy(row)` converts the DataSetRow to a NamedTuple
attrs(g)["Description of properties"] = """ Area units (`area`, `convex_area`) are in sq. kilometers, length units (`minor_axis_length`, `major_axis_length`, and `perimeter`) in kilometers, and `orientation` in radians (see the description of properties attribute.) Latitude and longitude coordinates are in degrees, and the stereographic coordinates`x` and `y` are in meters relative to the NSIDC north polar stereographic projection. Generated using the `regionprops` function from the `skimage` package. See https://scikit-image.org/docs/0.20.x/api/skimage.measure.html#regionprops
"""

mx = maximum(labeled_)
T = choose_dtype(mx)
# write_dataset(g, "labeled_image", T.(labeled_))
imgdata = T.(permutedims(labeled_))
obj, dtype = create_dataset(g, "labeled_image", imgdata)
attrs(obj)["CLASS"] = "IMAGE"
attrs(obj)["IMAGE_SUBCLASS"] = "IMAGE_INDEXED"
attrs(obj)["IMAGE_MINMAXRANGE"] = [minimum(imgdata), maximum(imgdata)]

attrs(obj)["description"] = "Connected components of the segmented floe image using a 3x3 structuring element. The property matrix consists of the properties of each connected component."
write_dataset(obj, dtype, imgdata)

end
return nothing
end
4 changes: 3 additions & 1 deletion test/test-IFTPipeline.jl-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ do
FALSECOLOR=${DATA_TARGET}/20220914.${satellite}.falsecolor.250m.tiff
SEGMENTED=${DATA_TARGET}/20220914.${satellite}.segmented.250m.tiff
FLOEPROPERTIES=${DATA_TARGET}/20220914.${satellite}.segmented.250m.props.csv
HDF5FILE=${DATA_TARGET}/20220914.${satellite}.h5
${IFT} preprocess_single --truecolor ${TRUECOLOR} --falsecolor ${FALSECOLOR} --landmask ${LANDMASK_NON_DILATED} --landmask-dilated ${LANDMASK_DILATED} --output ${SEGMENTED}
${IFT} extractfeatures_single --input ${SEGMENTED} --output ${FLOEPROPERTIES}
${IFT} makeh5files_single --passtime "2022-09-14T12:00:00" --truecolor ${TRUECOLOR} --falsecolor ${FALSECOLOR} --labeled ${SEGMENTED} --props ${FLOEPROPERTIES} --output ${HDF5FILE}
done

${IFT} track_single --imgs "${DATA_TARGET}/20220914.{aqua,terra}.segmented.250m.tiff" --props "${DATA_TARGET}/20220914.{aqua,terra}.segmented.250m.props.csv" --latlon ${TRUECOLOR} --passtimes "2022-09-14T12:00:00" "2022-09-15T12:00:00" --output ${DATA_TARGET}/paired-floes.csv
${IFT} track_single --imgs ${DATA_TARGET}/20220914.{aqua,terra}.segmented.250m.tiff --props ${DATA_TARGET}/20220914.{aqua,terra}.segmented.250m.props.csv --latlon ${TRUECOLOR} --passtimes "2022-09-14T12:00:00" "2022-09-15T12:00:00" --output ${DATA_TARGET}/paired-floes.csv



Expand Down
Loading