#!/bin/bash

if [ $# -ne 1 ]; then echo "Usage: $0 <fits_file>"; exit; fi
file_in=$1
basename=$(basename $file_in)
figName=$(echo $basename | cut -d. -f1 | cut -d_ -f1 | sed s/'-'/' '/g)
if [ $(echo $figName | wc -w) -gt 1 ]; then pmf=$(echo $figName | sed s/spec// | awk 'BEGIN{OFS = ","}{$1=$1+0}1 {$2=$2+0}2 {$3=$3+0}3'); name=$(grep -iw $pmf sample_obj_pmf_z_sn.csv | awk -F, '{print $1}');
#pmfcomma=$(echo $pmf | awk '{$1=$1+0}1 {$2=$2+0}2  {$3=$3+0}3' | sed s/' '/','/g);
else name="$figName"; pmf=$(grep -w $name sample_obj_pmf_z_sn.csv | awk -F, 'BEGIN{OFS = ","}{print $7*1,$8*1,$9*1}');
fi
redshift="";
redshift="$(grep -iw $name sample_obj_pmf_z_sn.csv | awk -F, '($6<1){print $6}')" ; 
if [ -z "$redshift" ]; then echo "$0: Incorrect redshift"; exit; fi

if [ -e tmp.fits ]; then rm tmp.fits; fi
ln -s $file_in tmp.fits
if [ -e tmp.dat ]; then rm tmp.dat; fi
# Izracunava sigma, h3, h4 sa defoltnim bias-om
sed -e 's/'redz'/'$redshift'/g' -e s/'\_elodie'/'\_elodie\_tmp'/g ppxf_kinematics_example_sdss_elodie.pro > ppxf_kinematics_example_sdss_elodie_tmp.pro

#/home/ana/idlcode/idl/bin/idl <<EOF
#.compile ppxf_kinematics_example_sdss_elodie_tmp.pro
#ppxf_kinematics_example_sdss_elodie_tmp
#EOF
/home/ana/idlcode/idl/bin/idl -e "ppxf_kinematics_example_sdss_elodie_tmp" -args $name

mkdir -p elodie/Figures
mv idl.ps elodie/Figures/$name.ps
echo $name > obj.dat
if ! [ -e tmp.dat ]; then echo "-9999     -9999     -9999     -9999     -9999     -9999     -9999     -9999    -9999    -9999     null     " > tmp.dat; fi
paste obj.dat tmp.dat > line.dat
paste line.dat >> elodie/veldisp.dat
