#!/bin/sh

export name=$1
name="$(printf '%s\n' "$name" | sed -e "s/\.ised//")"
typeset -i option=$2

# Delete old files
\rm -f $name.sed $name.indices $name.photometry $name.physical_properties

# Extract sed's for all time steps and write file $name.sed
$bc03/galaxevpl $name -all << END
          

END
cat $name.all | grep -v '\#'   > a.tmp1
cat $name.all | grep 'lambda'  > a.tmp0
cat a.tmp0 a.tmp1 > $name.sed
\rm -f a.tmp? $name.all
echo BUILT FILE: $name.sed
wc $name.sed
echo ''

# Build single table with all line indices in file $name.indices
cat $name.6lsindx_sed  | grep age     > 6.tmp0
cat $name.6lsindx_sed  | grep -v '\#' > 6.tmp1
cat 6.tmp0 6.tmp1 > 6.tmp
cat $name.7lsindx_sed  | grep age     > 7.tmp0
cat $name.7lsindx_sed  | grep -v '\#' > 7.tmp1
cat 7.tmp0 7.tmp1 > 7.tmp
cat $name.8lsindx_sed  | grep log_t   > u.tmp0
cat $name.8lsindx_sed  | grep -v '\#' > u.tmp1
cat u.tmp0 u.tmp1 > u.tmp
cut -c 11- 7.tmp  > 7.tmp2
cut -c 12- u.tmp  > u.tmp2
paste -d \  6.tmp 7.tmp2 > a.tmp
paste -d \  a.tmp u.tmp2 > b.tmp
\mv b.tmp $name.indices
\rm -f 6.tmp* 7.tmp* u.tmp* a.tmp*
echo BUILT FILE: $name.indices
wc $name.indices
echo ''

# Build single table with all photometric magnitudes in file $name.photometry
\mv fort.1000 $name.photometry
echo BUILT FILE: $name.photometry
wc $name.photometry
echo ''

# Build single table with all physical properties in file $name.physical_properties
cat $name.4color  | grep age   > 4.tmp0
cat $name.4color  | grep -v '\#' > 4.tmp1
cat 4.tmp0 4.tmp1 > 4.tmp
cut -c 51- 4.tmp  > 4.tmp2
cat $name.5color  | grep age   > 5.tmp0
cat $name.5color  | grep -v '\#' > 5.tmp1
cat 5.tmp0 5.tmp1 > 5.tmp
cut -c 21- 5.tmp  > 5.tmp2
cat $name.3color  | grep age   > 3.tmp0
cat $name.3color  | grep -v '\#' > 3.tmp1
cat 3.tmp0 3.tmp1 > 3.tmp
cut -c  1-11  3.tmp  > 3.tmpt	# cut -c  1-11  3.tmp  > 3.tmpt
cut -c 12-20  3.tmp  > 3.tmpd	# cut -c 12-21  3.tmp  > 3.tmpd
cut -c 21-38  3.tmp  > 3.tmpc	# cut -c 22-41  3.tmp  > 3.tmpc
cut -c 39-49  3.tmp  > 3.tmpb	# cut -c 42-53  3.tmp  > 3.tmpb
cut -c 50-85  3.tmp  > 3.tmpa	# cut -c 55-95  3.tmp  > 3.tmpa
cut -c 86-205 3.tmp  > 3.tmpu	# cut -c 96-178 3.tmp  > 3.tmpu
paste -d \  3.tmpt 3.tmpa 3.tmpb 3.tmpc 3.tmpd 3.tmpu 4.tmp2 5.tmp2 > b.tmp
\rm -f 3.tmp* 4.tmp* 5.tmp*
\mv b.tmp $name.physical_properties
echo BUILT FILE: $name.physical_properties
wc $name.physical_properties
echo ''

# Table with age for all time steps
\cp fort.2001 $name.time_tsteps
echo BUILT FILE: $name.time_tsteps
wc $name.time_tsteps
echo ''

# Build fits file
# $STILTS/stilts.sh  -verbose  tmulti ifmt=ascii   ofmt=fits  in=$name.sed  in=$name.physical_properties  in=$name.photometry  in=$name.indices  in=$name.time_tsteps  out=$name.fits
stilts -verbose  tmulti ifmt=ascii   ofmt=fits  in=$name.sed  in=$name.physical_properties  in=$name.photometry  in=$name.indices  in=$name.time_tsteps  out=$name.fits

# Update headers to make them fits compatible
$bc03/rebuildtimeheader.py $name.fits > fitszero
cat $bc03/rebuildfitsheader.py ./fitszero > fitszero.py
chmod a+x ./fitszero.py
./fitszero.py $name.fits $option

\mv newdata.fits $name.fits
echo ''
echo BUILT FILE: $name.fits
echo '---------------------------------------------------------------------------------------------'
echo ''

# Delete temporary files
\rm -f $name.sed  $name.physical_properties  $name.photometry  $name.indices fitszero*
\rm -f *.5color *.*lsindx* *.time_tsteps *.w_age_rf
\rm -f bc03.rm mix_stelib.tmp mix_stelib2.tmp csp_fits.tmp fort.*
