|
| 1 | +#!/bin/bash |
| 2 | +# Script to check NetCDF files for corruption |
| 3 | +# Usage: ./check_netcdf_files.sh /path/to/etopo_15s |
| 4 | + |
| 5 | +DATA_DIR="${1:-./data/etopo_15s}" |
| 6 | + |
| 7 | +echo "Checking NetCDF files in: $DATA_DIR" |
| 8 | +echo "========================================" |
| 9 | + |
| 10 | +corrupted_files=() |
| 11 | +total_files=0 |
| 12 | +checked_files=0 |
| 13 | + |
| 14 | +for file in "$DATA_DIR"/*.nc; do |
| 15 | + if [ -f "$file" ]; then |
| 16 | + total_files=$((total_files + 1)) |
| 17 | + filename=$(basename "$file") |
| 18 | + |
| 19 | + # Check if file size is suspiciously small (< 100KB likely corrupted) |
| 20 | + filesize=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null) |
| 21 | + |
| 22 | + if [ "$filesize" -lt 100000 ]; then |
| 23 | + echo "⚠️ SMALL FILE: $filename (${filesize} bytes)" |
| 24 | + corrupted_files+=("$file") |
| 25 | + else |
| 26 | + # Try to open with ncdump |
| 27 | + if ncdump -h "$file" > /dev/null 2>&1; then |
| 28 | + checked_files=$((checked_files + 1)) |
| 29 | + echo "✓ OK: $filename" |
| 30 | + else |
| 31 | + echo "✗ CORRUPTED: $filename" |
| 32 | + corrupted_files+=("$file") |
| 33 | + fi |
| 34 | + fi |
| 35 | + fi |
| 36 | +done |
| 37 | + |
| 38 | +echo "" |
| 39 | +echo "========================================" |
| 40 | +echo "Total files: $total_files" |
| 41 | +echo "Valid files: $checked_files" |
| 42 | +echo "Corrupted/suspect files: ${#corrupted_files[@]}" |
| 43 | + |
| 44 | +if [ ${#corrupted_files[@]} -gt 0 ]; then |
| 45 | + echo "" |
| 46 | + echo "Files to re-download:" |
| 47 | + for file in "${corrupted_files[@]}"; do |
| 48 | + echo " - $(basename "$file")" |
| 49 | + done |
| 50 | + |
| 51 | + echo "" |
| 52 | + read -p "Delete corrupted files? (yes/no): " delete_confirm |
| 53 | + if [ "$delete_confirm" = "yes" ]; then |
| 54 | + for file in "${corrupted_files[@]}"; do |
| 55 | + echo "Deleting: $(basename "$file")" |
| 56 | + rm "$file" |
| 57 | + done |
| 58 | + echo "Deleted ${#corrupted_files[@]} corrupted files" |
| 59 | + echo "Re-run the download script to fetch them again" |
| 60 | + fi |
| 61 | +fi |
0 commit comments