-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrop_all.sh
More file actions
executable file
·81 lines (67 loc) · 2.28 KB
/
crop_all.sh
File metadata and controls
executable file
·81 lines (67 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh
# crop_all.sh
# Script to run Rscript crop.R for all TIF files in data directory and subdirectories
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "Starting batch croping of TIF files..."
echo "=========================================="
# Check if R packages are available
printf "Checking R package dependencies...\n"
if ! R -e "library(terra)" >/dev/null 2>&1; then
printf "${RED}Error: terra package is not installed${NC}\n"
printf "Please install required R packages by running:\n"
printf " R -e \"install.packages(c('terra', 'sf', 'ggplot2', 'tidyverse'))\"\n"
printf "Or install them through RStudio's package manager.\n"
exit 1
fi
printf "${GREEN}✓ R package dependencies found${NC}\n"
echo
# Find all .tif files in data directory and subdirectories
tif_files=$(find data -name "*.tif" -type f)
# Check if any TIF files were found
if [ -z "$tif_files" ]; then
printf "${RED}No TIF files found in data directory${NC}\n"
exit 1
fi
# Count total files
total_files=$(echo "$tif_files" | wc -l)
printf "${YELLOW}Found $total_files TIF files to crop${NC}\n"
echo
# Initialize counters
croped=0
failed=0
# crop each TIF file
while IFS= read -r tif_file; do
croped=$((croped + 1))
printf "${YELLOW}croping ($croped/$total_files): $tif_file${NC}\n"
# Check if corresponding .txt file exists
txt_file="${tif_file}.txt"
if [ ! -f "$txt_file" ]; then
printf "${RED} Error: Missing coordinate file: $txt_file${NC}\n"
failed=$((failed + 1))
continue
fi
# Run Rscript crop.R with the TIF file
# Set R library path to include common RStudio package locations
if Rscript crop.R "$tif_file"; then
printf "${GREEN} ✓ Successfully croped: $tif_file${NC}\n"
else
printf "${RED} ✗ Failed to crop: $tif_file${NC}\n"
failed=$((failed + 1))
fi
echo
done <<< "$tif_files"
# Summary
echo "=========================================="
printf "${YELLOW}croping complete!${NC}\n"
printf "Total files: $total_files\n"
printf "${GREEN}Successfully croped: $((croped - failed))${NC}\n"
if [ $failed -gt 0 ]; then
printf "${RED}Failed: $failed${NC}\n"
exit 1
else
printf "${GREEN}All files croped successfully!${NC}\n"
fi