From 1df755fab9f48a3f1dddab745c87bad560364d2a Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Mon, 4 Nov 2024 22:53:09 +0000 Subject: [PATCH 1/3] add single file version of makeh5files CLI --- IFTPipeline.jl/src/cli.jl | 44 +++++++++++++++++++++++++++++ IFTPipeline.jl/src/h5.jl | 50 +++++++++++++++++++++++++++++++++ test/test-IFTPipeline.jl-cli.sh | 1 + 3 files changed, 95 insertions(+) diff --git a/IFTPipeline.jl/src/cli.jl b/IFTPipeline.jl/src/cli.jl index ff41e5b1..7ff65c75 100755 --- a/IFTPipeline.jl/src/cli.jl +++ b/IFTPipeline.jl/src/cli.jl @@ -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 @@ -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!, ) @@ -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 diff --git a/IFTPipeline.jl/src/h5.jl b/IFTPipeline.jl/src/h5.jl index 7b501fd9..68e0c5dd 100644 --- a/IFTPipeline.jl/src/h5.jl +++ b/IFTPipeline.jl/src/h5.jl @@ -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"] = "mmwilhelmus@brown.edu" + + 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 \ No newline at end of file diff --git a/test/test-IFTPipeline.jl-cli.sh b/test/test-IFTPipeline.jl-cli.sh index 3db1a86a..8f326629 100755 --- a/test/test-IFTPipeline.jl-cli.sh +++ b/test/test-IFTPipeline.jl-cli.sh @@ -38,6 +38,7 @@ 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} done From da5682e8264cb4c9efb7b22b7acbc29681658b02 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Mon, 4 Nov 2024 23:05:16 +0000 Subject: [PATCH 2/3] fix track_single call --- test/test-IFTPipeline.jl-cli.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test-IFTPipeline.jl-cli.sh b/test/test-IFTPipeline.jl-cli.sh index 8f326629..6eda63ec 100755 --- a/test/test-IFTPipeline.jl-cli.sh +++ b/test/test-IFTPipeline.jl-cli.sh @@ -43,7 +43,7 @@ do ${IFT} extractfeatures_single --input ${SEGMENTED} --output ${FLOEPROPERTIES} 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 From a8bd5cef9c5838f09b953515b52fca294e866251 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Mon, 4 Nov 2024 23:05:31 +0000 Subject: [PATCH 3/3] add makeh5files_single case --- test/test-IFTPipeline.jl-cli.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test-IFTPipeline.jl-cli.sh b/test/test-IFTPipeline.jl-cli.sh index 6eda63ec..40abbac8 100755 --- a/test/test-IFTPipeline.jl-cli.sh +++ b/test/test-IFTPipeline.jl-cli.sh @@ -41,6 +41,7 @@ do 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