#!/usr/bin/env bash
set -euo pipefail

usage() {
    cat <<'EOF'
Usage:
  ./run_ff_plot.sh one
  ./run_ff_plot.sh two
  ./run_ff_plot.sh both

Optional environment overrides:
  PYTHON_CMD=python3
  PLOT_SCRIPT=/path/to/ff_slip_dist_latlon.py
  ONE_COMBINED_FILE=/path/to/one_segment.plt
  TWO_COMBINED_FILE=/path/to/two_segment.plt
EOF
}

[[ $# -eq 1 ]] || { usage; exit 2; }
CASE_NAME=$1

#PYTHON_CMD=${PYTHON_CMD:-python}
PYTHON_CMD=${PYTHON_CMD:-/data/seis01/taira/miniconda3/envs/netops/bin/python}

PLOT_SCRIPT=${PLOT_SCRIPT:-ff_slip_dist_latlon.py}

# Change ONE_COMBINED_FILE here if the one-segment filename is different.
ONE_COMBINED_FILE=${ONE_COMBINED_FILE:-reg_inv.out.inc_gps_rakeval_multime30.plt}
TWO_COMBINED_FILE=${TWO_COMBINED_FILE:-reg_inv.out.inc_gps_seg2_rdf3_side3_rakeval_multime30.plt}

SEIS_CATALOG=../hypodd_catalog/NCAeqDDRT.v202201.downloaded.20260624
REPEATING_CATALOG=MTJ.freq4.0-16.0Hz_maxdist60.0_coh9700_linkage_cluster.txt
SLAB_DIR=/data/seis02/taira/BSL_MT_Training/mendo2024/ff_plot/Slab2Distribute_Mar2018

COMMON_ARGS=(
    --make_plot
    --use_moment_for_slip
    --slip_cmax 400
    --marker_size 125
    --plot_mu_depth
    --plot_mu_compare

    --plot_seismicity
    --seis_catalog "$SEIS_CATALOG"
    --seis_start_time 2024-12-05T18:44:21
    --seis_end_time 2025-12-05T18:44:21
    --seis_min_depth 0
    --seis_max_depth 50
    --seis_min_mag 1.5
    --seis_edgecolor blue
    --seis_facecolor none
    --seis_max_across_km 10
    --seis_marker_ddrt30 o
    --seis_marker_hinv D
    --seis_marker_other x
    --seis_scale_by_mag
    --seis_marker_size 30
    --seis_marker_mag_ref 3.0
    --seis_marker_mag_scale 3.0
    --seis_marker_size_min 8
    --seis_marker_size_max 220
    --seis_depth_offset_km 0.293
    --ignore_across_strike_distance_depth_projection
    --seis_legend
    --seis_legend_loc "lower left"

    --plot_repeating
    --repeating_catalog "$REPEATING_CATALOG"
    --repeating_facecolor red

    --plot_slab
    --slabdir "$SLAB_DIR"
    --slab_id cas
    --slab_color black
    --slab_sample_backend gmt
    --gmt_cmd gmt
    --slab_sample_dx_km 1.0

    --make_map_plot
    --map_plot_seismicity
    --map_plot_repeating

    --no_map_plot_slip
    #--map_plot_slip


    --map_plot_target_event \
    --target_event_lat 40.374 \
    --target_event_lon -125.022 \
    --target_event_model_depth_km 9.0 \
    
    --target_event_cross_section

    # Fixed bounds for consistent one/two-segment comparisons
    # --map_min_lon -125.4
    # --map_max_lon -124.5
    # --map_min_lat 40.30
    # --map_max_lat 40.45

    # --map_min_lon -125.5
    # --map_max_lon -123.5
    # --map_min_lat 40.00
    # --map_max_lat 41.00

    --map_min_lon -125.6
    --map_max_lon -124.2
    --map_min_lat 40.10
    --map_max_lat 40.50

    # Slip cells reconstructed from actual inversion lon/lat coordinates
    --map_fault_render midpoint_cells
    --map_midpoint_cell_edgecolor none
    --map_midpoint_cell_linewidth 0
    --map_midpoint_cell_alpha 1.0
    --map_midpoint_cell_zorder 4

    # Segment outlines
    --map_midpoint_draw_outlines
    --map_segment1_outline_color black
    --map_segment2_outline_color gray
    --map_segment1_outline_linewidth 1.25
    --map_segment2_outline_linewidth 1.25
    --map_segment1_outline_linestyle solid
    --map_segment2_outline_linestyle dashed

    --map_segment_outline_zorder 14

    # Top trace is unnecessary when full outlines are shown
    --no_map_draw_top_trace

    # Map scale
    --map_scale_bar_km 20
    --map_scale_bar_loc lower_right
    --map_scale_bar_x 0.08
    --map_scale_bar_y 0.06


    # Common absolute marker areas
    --seis_legend_marker_size 48
    --map_legend_marker_size 48

    # Keep the older global multipliers neutral
    --seis_legend_markerscale 1.0
    --map_legend_markerscale 1.0

    # Per-symbol visual compensation
    --legend_scale_hinv 1.0
    --legend_scale_ddrt30 1.0
    --legend_scale_repeating_hinv 1.0
    --legend_scale_repeating_ddrt30 1.0
    --legend_scale_target 2.0


    # Coastline
    --map_coastline_source cartopy
    --map_coastline_resolution 10m
    --map_coastline_color 0.35
    --map_coastline_linewidth 0.8

    # Longitude/latitude ticks and guide lines
    --map_lon_tick_interval 0.2
    --map_lat_tick_interval 0.05
    --map_tick_label_size 9
    --map_gridlines
    --map_gridline_color 0.75
    --map_gridline_linewidth 0.5
    --map_gridline_alpha 0.6
    --map_gridline_linestyle dotted



    # Cross-section legend
    --cross_section_legend_mode segment1_combined

    --legend_label_hinv "hypoinv"
    --legend_label_ddrt30 "DDRT"

    --legend_label_repeating_hinv "repeating EQ (hypoinv)"
    --legend_label_repeating_ddrt30 "repeating EQ (DDRT)"

    --legend_label_slab "Slab2 interface"
    --legend_label_target "2024 M7.0 EQ"

    --map_legend_label_segment1 "segment 1"
    --map_legend_label_segment2 "segment 2"

    --map_legend_label_hinv "hypoinv"
    --map_legend_label_ddrt30 "DDRT"

    --map_legend_label_repeating_hinv "repeating EQ (hypoinv)"
    --map_legend_label_repeating_ddrt30 "repeating EQ (DDRT)"

    --map_legend_label_target "2024 M7.0 EQ"
    --map_legend_label_slab "Slab2 interface"

)

run_one() {
    local prefix=plot_test_one_segment
    local case_args=(
        --combined_file "$ONE_COMBINED_FILE"
        --rupture_config rakeval_multime3

        --xgrid_num 40
        --zgrid_num 20
        --x_length_km 80
        --z_length_km 40

        --strike1_deg 275
        --seis_strike1_deg -275
        --dip1_deg 84

        --panel_wspace 0
        --panel_width_scale 1.0

        --output_prefix "$prefix"
        --plot_file "${prefix}.pdf"
        --seis_output "${prefix}.selected_seismicity.csv"
        --repeating_output "${prefix}.selected_repeating.csv"
        --slab_output "${prefix}.slab_profile.csv"

        --map_plot_file "${prefix}.map.pdf"

    )

    echo "# Running one-segment case"
    echo "# Input:  $ONE_COMBINED_FILE"
    echo "# Output: $prefix"
    "$PYTHON_CMD" "$PLOT_SCRIPT" "${COMMON_ARGS[@]}" "${case_args[@]}"
}

run_two() {
    local prefix=plot_test_two_segments
    local case_args=(
        --combined_file "$TWO_COMBINED_FILE"
        --rupture_config seg2_rakeval_multime3

        --xgrid_num 20
        --zgrid_num 20
        --x_length_km 40
        --z_length_km 40

        # Segment 2 inherits the segment 1 grid unless these are changed.
        --xgrid_num2 20
        --zgrid_num2 20
        --x_length_km2 40
        --z_length_km2 40

        --strike1_deg 275
        --strike2_deg 280
        --seis_strike1_deg -275
        --seis_strike2_deg -280
        --dip1_deg 84
        --dip2_deg 84

        --panel_wspace 0
        --panel_width_scale 0.75

        --output_prefix "$prefix"
        --plot_file "${prefix}.pdf"
        --seis_output "${prefix}.selected_seismicity.csv"
        --repeating_output "${prefix}.selected_repeating.csv"
        --slab_output "${prefix}.slab_profile.csv"

        --map_plot_file "${prefix}.map.pdf"
    )

    echo "# Running two-segment case"
    echo "# Input:  $TWO_COMBINED_FILE"
    echo "# Output: $prefix"
    "$PYTHON_CMD" "$PLOT_SCRIPT" "${COMMON_ARGS[@]}" "${case_args[@]}"
}

case "$CASE_NAME" in
    one)  run_one ;;
    two)  run_two ;;
    both) run_one; run_two ;;
    *)
        echo "ERROR: unknown case: $CASE_NAME" >&2
        usage >&2
        exit 2
        ;;
esac
