#!/bin/bash
# --- Configuration ---
# Fixed paths and parameters for your Python script
EXP_PREFIX="Eta10_C00"
EXP_INIT_CONDITION="2015020200" # This seems to be a fixed part of the input NetCDF path
NETCDF_DIR="/share/grpeta/dsk001/dist/users/jorge/cicloneBapo/${EXP_INIT_CONDITION}/Eta10km_Topo1hPa/netcdf"
COG_GEOTIF_DIR="/share/grpeta/dsk001/dist/users/jorge/cicloneBapo/cog_geotif"

# Define arrays for variables and levels
# Variables that are typically 2D (surface variables) and don't need a level argument
VARIABLES_2D=() 

# Variables that are typically 3D/4D (atmospheric variables) and require a level argument
VARIABLES_3D=("ZGEO" "UVEL" "VVEL" "TEMP" "UMRL" "OMEG" "UMES" "PSAT")

# Levels to process for 3D variables (as strings, matching your example '000750')
LEVELS=("001020" "001000" "000950" "000925" "000900" "000850" "000800" "000750" \
        "000700" "000650" "000600" "000550" "000500" "000450" "000400" "000350" \
        "000300" "000250" "000200" "000150" "000100" "000050")

# Parallel processing control
MAX_CONCURRENT_JOBS=10 # Adjust this number based on your CPU cores and system resources

# --- Main Processing Loop ---
 
echo "Starting parallel COG conversion process..."
echo "NetCDF Input Directory: ${NETCDF_DIR}"
echo "COG Output Directory: ${COG_GEOTIF_DIR}"
echo "Max Concurrent Jobs: ${MAX_CONCURRENT_JOBS}"

JOB_COUNT=0

# Process 2D variables (without explicit level argument)
for var_2d in "${VARIABLES_2D[@]}"; do
    echo "Processing 2D variable: ${var_2d}"
    
    # Construct the command for 2D variables
    # Assuming nc2coggeotif_v2.py expects:
    # python3 script.py <exp_prefix> <exp_init_condition> <variable> <input_dir> <output_dir>
    # And handles the lack of --level_index gracefully
    CMD="python3 nc2coggeotif_v2.py ${EXP_PREFIX} ${EXP_INIT_CONDITION} ${var_2d} ${NETCDF_DIR} ${COG_GEOTIF_DIR}"
    
    echo "Running: ${CMD}"
    ${CMD} & # Run in background
    PID=$! # Get the Process ID of the last background command
    echo "Started PID: ${PID} for ${var_2d}"
    JOB_COUNT=$((JOB_COUNT + 1))

    # Check if we've reached the max concurrent jobs
    if (( JOB_COUNT >= MAX_CONCURRENT_JOBS )); then
        echo "Max concurrent jobs reached (${MAX_CONCURRENT_JOBS}). Waiting for a job to finish..."
        wait -n # Wait for the next background job to complete
        JOB_COUNT=$((JOB_COUNT - 1)) # Decrement count as one job finished
    fi
done

# Process 3D variables (with explicit level argument)
for var_3d in "${VARIABLES_3D[@]}"; do
    echo "Processing 3D variable: ${var_3d}"
    for level in "${LEVELS[@]}"; do
        echo "  - Level: ${level}"

        # Construct the command for 3D variables
        # Assuming nc2coggeotif_v2.py expects:
        # python3 script.py <exp_prefix> <exp_init_condition> <variable> <input_dir> <output_dir> --level_index <level_str>
        # Note: The level string '000750' might need to be converted to an integer index (e.g., 0, 1, 2...)
        # inside your Python script based on its internal logic.
        # For simplicity, I'm passing the string '000750' as the level argument.
        # If your Python script expects a numeric index, you'll need to map this string to an int in Python.
        
        # If your Python script's argparse expects the level as a positional argument like:
        # python3 script.py <exp_prefix> <exp_init_condition> <variable> <level> <input_dir> <output_dir>
        # then remove '--level_index' below.
        CMD="python3 nc2coggeotif_v2.py ${EXP_PREFIX} ${EXP_INIT_CONDITION} ${var_3d} ${NETCDF_DIR} ${COG_GEOTIF_DIR} --level ${level}"
        
        echo "Running: ${CMD}"
        ${CMD} & # Run in background
        PID=$! # Get the Process ID of the last background command
        echo "Started PID: ${PID} for ${var_3d} level ${level}"
        JOB_COUNT=$((JOB_COUNT + 1))

        # Check if we've reached the max concurrent jobs
        if (( JOB_COUNT >= MAX_CONCURRENT_JOBS )); then
            echo "Max concurrent jobs reached (${MAX_CONCURRENT_JOBS}). Waiting for a job to finish..."
            wait -n # Wait for the next background job to complete
            JOB_COUNT=$((JOB_COUNT - 1)) # Decrement count as one job finished
        fi
    done
done

# Wait for all remaining background jobs to complete
echo "All jobs submitted. Waiting for remaining jobs to finish..."
wait
echo "All COG conversion processes completed."
