#!/bin/bash
# make_synoptic_links.sh
# 批量把 synoptic 下各年份目录里的 crXXXX 文件软链接到 synoptic/ 根目录
# 链接名格式：fmg.Synoptic_Ml.XXXX.fits.gz

set -e

BASE_DIR="/data1/fmg/fmg_test/asos/data/fmg/science/synoptic"

find "$BASE_DIR" -mindepth 2 -maxdepth 2 -type f -name "*_cr*_*.fits.gz" | while read -r SRC; do
    CR_NUM=$(basename "$SRC" | grep -oP 'cr\K[0-9]+' | head -1)

    if [ -z "$CR_NUM" ]; then
        echo "跳过（无法提取 cr 编号）: $SRC"
        continue
    fi

    DST="${BASE_DIR}/fmg.Synoptic_Ml.${CR_NUM}.fits.gz"

    if [ -e "$DST" ] || [ -L "$DST" ]; then
        rm -f "$DST"
    fi

    ln -s "$SRC" "$DST"
    echo "链接: $DST -> $SRC"
done

echo "完成。"
