#!/bin/bash

SUFFIX="$1"

CAT="/home/bourgesl/JMMC_SCL/2023/jsdc-2305-${SUFFIX}.fits"

if [ ! -e "$CAT" ]
then
    echo "Missing file: '$CAT'"
    exit 1
else
    echo "Processing $CAT"
fi

source ~/test-jdk17.sh

# use headless mode
STILTS_JAVA_OPTIONS="-Xms4G -Xmx4G -XX:+AlwaysPreTouch -Djava.io.tmpdir=$HOME/tmp/ -Djava.awt.headless=true -verbose"
echo "Stilts options: $STILTS_JAVA_OPTIONS"

WX=1000
WY=800
WY2=500

CHI2_TH=5.01
CHI2_TH=10.01
echo "CHI2_TH: $CHI2_TH"

# Residu vs ldd:
# all
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY2 \
   xlog=true xlabel='LDD_1 (mas) - all stars' \
   ylabel='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   grid=true \
   xmin=0.001 xmax=20 ymin=-4 ymax=4 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH' " \
   x=LDD_1 \
   y='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   layer_1=Mark \
      shading_1=density densemap_1=magma densefunc_1=histolog \
   layer_2=Function \
      fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   out=res_vs_ldd-${SUFFIX}.png

# e_JHK <= 0.10
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY2 \
   xlog=true xlabel='LDD_1 (mas) - e_JHK <= 0.10' \
   ylabel='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   grid=true \
   xmin=0.001 xmax=20 ymin=-4 ymax=4 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH && max(e_j_2, max(e_h_2, e_k_2) ) <= 0.10' " \
   x=LDD_1 \
   y='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   layer_1=Mark \
      shading_1=density densemap_1=magma densefunc_1=histolog \
   layer_2=Function \
      fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   out=res_vs_ldd-${SUFFIX}_eJHK_low.png

# e_JHK > 0.10
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY2 \
   xlog=true xlabel='LDD_1 (mas) - e_JHK > 0.10' \
   ylabel='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   grid=true \
   xmin=0.001 xmax=20 ymin=-4 ymax=4 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH && max(e_j_2, max(e_h_2, e_k_2) ) > 0.10' " \
   x=LDD_1 \
   y='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   layer_1=Mark \
      shading_1=density densemap_1=magma densefunc_1=histolog \
   layer_2=Function \
      fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   out=res_vs_ldd-${SUFFIX}_eJHK_high.png




# Rel error vs color_table_index:
# all
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY2 \
   xlabel='color_table_index_2 - all stars' \
   ylog=true ylabel='e_LDD_2 / LDD_2' \
   grid=true \
   xmin=30 xmax=280 ymin=0.01 ymax=0.2 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 < 4.01 && diam_chi2_2 <= $CHI2_TH' " \
   x=color_table_index_2 \
   y='e_LDD_2 / LDD_2' \
   layer_1=Mark \
      shading_1=density size_1=9 color_1=blue densemap_1=magma densefunc_1=histolog \
   layer_2=Function \
      fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   layer_3=Quantile color_3=blue quantiles_3=0.4,0.5 thick_3=5 smooth_3=4.0 \
   out=rel_err_vs_color_index-${SUFFIX}.png

# e_JHK <= 0.10
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY2 \
   xlabel='color_table_index_2 - all stars' \
   ylog=true ylabel='e_LDD_2 / LDD_2' \
   grid=true \
   xmin=30 xmax=280 ymin=0.01 ymax=0.2 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH && max(e_j_2, max(e_h_2, e_k_2) ) <= 0.10' " \
   x=color_table_index_2 \
   y='e_LDD_2 / LDD_2' \
   layer_1=Mark \
      shading_1=density size_1=9 color_1=blue densemap_1=magma densefunc_1=histolog \
   layer_2=Function \
      fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   layer_3=Quantile color_3=blue quantiles_3=0.4,0.5 thick_3=5 smooth_3=4.0 \
   out=rel_err_vs_color_index-${SUFFIX}_eJHK_low.png

# e_JHK > 0.10
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY2 \
   xlabel='color_table_index_2 - all stars' \
   ylog=true ylabel='e_LDD_2 / LDD_2' \
   grid=true \
   xmin=30 xmax=280 ymin=0.01 ymax=0.2 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH && max(e_j_2, max(e_h_2, e_k_2) ) > 0.10' " \
   x=color_table_index_2 \
   y='e_LDD_2 / LDD_2' \
   layer_1=Mark \
      shading_1=density size_1=9 color_1=blue densemap_1=magma densefunc_1=histolog \
   layer_2=Function \
      fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   layer_3=Quantile color_3=blue quantiles_3=0.4,0.5 thick_3=5 smooth_3=4.0 \
   out=rel_err_vs_color_index-${SUFFIX}_eJHK_high.png



# Residu vs color_table_index:
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY2 \
   xlabel='color_table_index_2 - all stars' \
   ylabel='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   grid=true \
   xmin=30 xmax=280 ymin=-2 ymax=3 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01' " \
   x=color_table_index_2 \
   y='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   layer_1=Mark \
      shading_1=density size_1=9 color_1=blue densemap_1=magma densefunc_1=histolog \
   layer_2=Function \
      fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   layer_3=Quantile color_3=blue quantiles_3=0.4,0.5 thick_3=5 smooth_3=4.0 \
   out=res_vs_color_index-${SUFFIX}_all.png

stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY2 \
   xlabel='color_table_index_2 - good stars' \
   ylabel='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   grid=true \
   xmin=30 xmax=280 ymin=-2 ymax=3 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH && max(e_j_2, max(e_h_2, e_k_2) ) <= 0.10' " \
   x=color_table_index_2 \
   y='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   layer_1=Mark \
      shading_1=density size_1=9 color_1=blue densemap_1=magma densefunc_1=histolog \
   layer_2=Function \
      fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   layer_3=Quantile color_3=blue quantiles_3=0.4,0.5 thick_3=5 smooth_3=4.0 \
   out=res_vs_color_index-${SUFFIX}_good.png

for i in -1 1 2 3 4 5; do
    stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
       xpix=$WX ypix=$WY2 \
       xlabel="color_table_index_2 - Lum Class = $i" \
       ylabel='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
       grid=true \
       xmin=30 xmax=280 ymin=-2 ymax=3 \
       legend=false \
       in=$CAT \
       icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH && max(e_j_2, max(e_h_2, e_k_2) ) <= 0.10 && lum_class_2 == $i' " \
       x=color_table_index_2 \
       y='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
       layer_1=Mark \
          shading_1=density size_1=9 color_1=blue densemap_1=magma densefunc_1=histolog \
       layer_2=Function \
          fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
       layer_3=Quantile color_3=blue quantiles_3=0.4,0.5 thick_3=5 smooth_3=4.0 \
       out=res_vs_color_index-${SUFFIX}_lumclass_${i}.png
done




# Histogram relative Diff LDD:
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY \
   ylog=true xlabel='2.0*(LDD_2 - LDD_1) / (LDD_2 + LDD_1)' ylabel= grid=true \
   xmin=-0.1 xmax=0.1 ymin=5 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH && max(e_j_2, max(e_h_2, e_k_2) ) <= 0.10' " \
   layer_1=Histogram \
      x_1='2.0*(LDD_2 - LDD_1) / (LDD_2 + LDD_1)' \
      binsize_1=-229 phase_1=0.5 barform_1=semi_steps \
   layer_2=Function \
      axis_2=Vertical fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   out=hist_rel_diff-${SUFFIX}.png




# Histogram Residus:
stilts ${STILTS_JAVA_OPTIONS} plot2plane texttype=antialias \
   xpix=$WX ypix=$WY \
   ylog=true xlabel='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
   ylabel= grid=true \
   xmin=-2 xmax=3 ymin=5 \
   legend=false \
   in=$CAT \
   icmd="select 'color_table_delta_2 <= 4.01 && diam_chi2_2 <= $CHI2_TH && max(e_j_2, max(e_h_2, e_k_2) ) <= 0.10' " \
   layer_1=Histogram \
      x_1='(LDD_2 - LDD_1) / sqrt(e_LDD_1*e_LDD_1 + e_LDD_2*e_LDD_2)' \
      color_1=blue binsize_1=-168 phase_1=0.5 barform_1=semi_steps \
   layer_2=Function \
      axis_2=Vertical fexpr_2=0 color_2=green thick_2=2 antialias_2=true \
   out=hist_res-${SUFFIX}.png


