#!/bin/bash
# stelib_*[0-9][A-Z].fits
file_in=$1
base=$(basename $file_in)
name=$(echo ${base/.fits})

rm test.fits
# Creates simple 1d spectrum with single extension for midas
#/home/ana/idlcode/idl/bin/idl <<EOF
#spec=mrdfits('$file_in',0,h,/silent)
#wave=sxpar(h, 'CRVAL1') + dindgen(sxpar(h,'NAXIS1')-1) * sxpar(h, 'CDELT1')
#test=spec[where(wave gt 3780 and wave lt 9270)]
#if ((where(test eq 0.0))[0] ne 0) then writefits, 'test.fits', spec/median(test), h
#EOF

#if ! [ -e test.fits ];then echo "$file_in has gaps."; exit ; fi
cp $file_in test.fits
# Creates template files in [lowcut:upcut]AA limits without gaps 
low=$(gethead crval1 $file_in); step=$(gethead cdelt1 $file_in); step=$(printf "%3.1f\n" $step);
npix=$(gethead naxis1 $file_in); lowcut="3780"; upcut="9270"; median=$(grep -w $base stelib_gaps_edges.dat | awk '{print $NF}')
#To explore the full SDSS range templates should be cut in [3780:9270]AA.
echo $low $step $npix $lowcut $upcut
sed -e 's/lowlim/'$low'/g' -e 's/lowcut/'$lowcut'/g' -e 's/upcut/'$upcut'/g' -e 's/name/'$name'/g' -e 's/npix/'$npix'/g'  -e 's/step/'$step'/g' -e 's/median/'$median'/g' midas_stelib.prg > tmp.prg
/home/ana/midas/09FEBpl1.0/system/unix/inmidas -p 04 < tmp.prg
