#!/bin/bash

# 配置
TARGET_DIR="/data/public/lzy/inputdata"
BASE_URL="https://svn-ccsm-inputdata.cgd.ucar.edu/trunk/inputdata"
MAX_RETRIES=3
RETRY_DELAY=5

# 创建基础目录
mkdir -p "$TARGET_DIR"

# 进入目标目录
cd "$TARGET_DIR"

# 下载文件函数（支持断点续传和重试）
download_file_with_retry() {
    local relative_path="$1"
    local file_path="$TARGET_DIR/$relative_path"
    local url="$BASE_URL/$relative_path"
    local dir_name=$(dirname "$file_path")
    local retry_count=0
    
    # 创建目录
    mkdir -p "$dir_name"
    
    # 如果文件已存在且完整，跳过下载
    if [ -f "$file_path" ]; then
        local file_size=$(stat -c%s "$file_path" 2>/dev/null || stat -f%z "$file_path" 2>/dev/null)
        if [ "$file_size" -gt 0 ]; then
            echo "✓ 文件已存在: $relative_path ($file_size bytes)"
            return 0
        fi
    fi
    
    echo "下载: $relative_path"
    
    while [ $retry_count -lt $MAX_RETRIES ]; do
        # 使用wget下载，支持断点续传
        wget -c -q --show-progress -O "$file_path" "$url"
        
        if [ $? -eq 0 ]; then
            echo "✓ 成功下载: $relative_path"
            return 0
        else
            retry_count=$((retry_count + 1))
            if [ $retry_count -lt $MAX_RETRIES ]; then
                echo "✗ 下载失败，第 $retry_count 次重试: $relative_path"
                sleep $RETRY_DELAY
            else
                echo "✗ 最终下载失败: $relative_path"
                return 1
            fi
        fi
    done
}

# 记录开始时间
start_time=$(date +%s)
echo "========================================="
echo "开始下载CESM输入文件"
echo "时间: $(date)"
echo "目标目录: $TARGET_DIR"
echo "========================================="

# 定义要下载的文件列表
files_to_download=(
    # 1. pop模型文件
    "ocn/pop/gx1v7/forcing/EBM_param_gx1v7_JRA55.2017-11-16.nc"
    
    # 2. datm模型文件
    "atm/datm7/JRA55/domain.atm.TL319.151007.nc"
    
    # 3. domain文件
    "share/domains/domain.lnd.TL319_gx1v7.170705.nc"
    
    # 4. cpl模型文件
    "cpl/gridmaps/rJRA025/map_JRA025m_to_gx1v7_nn_open_ocean_nnsm_e1000r300_marginal_sea_190214.nc"
    "cpl/gridmaps/TL319/map_TL319_TO_gx1v7_aave.170705.nc"
    "cpl/gridmaps/rJRA025/map_JRA025m_to_gx1v7_e1000r300_190214.nc"
    "cpl/gridmaps/gx1v7/map_gx1v7_TO_TL319_aave.170705.nc"
    "cpl/gridmaps/TL319/map_TL319_TO_gx1v7_patc.170705.nc"
    "cpl/gridmaps/TL319/map_TL319_TO_gx1v7_blin.170705.nc"
    "cpl/gridmaps/TL319/map_TL319_TO_ww3a_bilin.170707.nc"
    
    # 5. cice模型文件
    "ice/cice/b.e15.B1850G.f09_g16.pi_control.25.cice.r.0041-01-01-00000.nc"
    
    # 6. drof模型文件
    "lnd/dlnd7/JRA55/domain.roff.JRA025.170111.nc"
)

# 下载单个文件
for file in "${files_to_download[@]}"; do
    download_file_with_retry "$file"
done

# 下载年度序列文件
echo "========================================="
echo "开始下载年度序列文件..."
echo "========================================="

# 定义年度序列文件类型和年份范围
#file_types=("prec" "lwdn" "swdn" "q_10" "slp" "t_10" "u_10" "v_10")
#file_types=("prec" "lwdn" "swdn" "q_10" "slp")
file_types=("t_10" "u_10" "v_10")
start_year=1958
end_year=2018

# 下载年度序列文件
for file_type in "${file_types[@]}"; do
    echo "下载 $file_type 文件..."
    for year in $(seq $start_year $end_year); do
        if [ "$file_type" = "runoff" ]; then
            download_file_with_retry "lnd/dlnd7/JRA55/JRA.v1.4.runoff.$year.190214.nc"
        else
            download_file_with_retry "ocn/jra55/v1.3_noleap/JRA.v1.3.$file_type.TL319.$year.190528.nc"
        fi
    done
done

# 计算下载时间
end_time=$(date +%s)
duration=$((end_time - start_time))
hours=$((duration / 3600))
minutes=$(( (duration % 3600) / 60 ))
seconds=$((duration % 60))

echo "========================================="
echo "下载完成！"
echo "结束时间: $(date)"
echo "总耗时: ${hours}小时 ${minutes}分钟 ${seconds}秒"
echo "文件已保存到: $TARGET_DIR"
echo "========================================="
