#!/bin/bash

# Create 60x 10 week interval scripts.
# We only need to change t1, t2, and the "for x in { .. }" loop in each script.

# Remove previously generated scripts
rm */1987A_*.sh

# time0 is the mission start time
time0=239557417

# We want 60 scripts in 10 week chuncks.
# For simplicity the scripts will be numbered from 0
# We just need to manually fix the 1987A_0.sh script afterwards.
for script in {0..60}

do

begin_week=$((script*10))
end_week=$((begin_week+9))

begin_time=$((time0+(script*6048000)))
end_time=$((begin_time+604800)) # 604800=1 week in seconds

{
   echo '#!/bin/bash'
   echo ''
   echo 't1='$begin_time''
   echo 't2='$end_time''
   echo ''
   echo 'for x in {'$begin_week'..'$end_week'}'
   echo ''
   echo 'do'
   echo ''
   echo '# Energy bins 800-2000 MeV; EA'
   echo '# Energy bins 2000-5000 MeV; EB'
   echo '# Energy bins >5000 MeV; EC'
   echo ''
   echo 'gtselect \'
   echo '  infile=@/data/ajm/new_fermi_data/events.txt \'
   echo '  outfile=/data/ajm/SN/'$script'/filtered_87A_EA_n$x.fits \'
   echo '  ra=0 dec=0 rad=180.0 evclass=128 evtype=3 \'
   echo '  tmin=$t1 \'
   echo '  tmax=$t2 \'
   echo '  emin=800 emax=2000 zmax=90'
   echo ''
   echo 'gtselect \'
   echo '  infile=@/data/ajm/new_fermi_data/events.txt \'
   echo '  outfile=/data/ajm/SN/'$script'/filtered_87A_EB_n$x.fits \'
   echo '  ra=0 dec=0 rad=180.0 evclass=128 evtype=3 \'
   echo '  tmin=$t1 \'
   echo '  tmax=$t2 \'
   echo '  emin=2000 emax=5000 zmax=90'
   echo ''
   echo 'gtselect \'
   echo '  infile=@/data/ajm/new_fermi_data/events.txt \'
   echo '  outfile=/data/ajm/SN/'$script'/filtered_87A_EC_n$x.fits \'
   echo '  ra=0 dec=0 rad=180.0 evclass=128 evtype=3 \'
   echo '  tmin=$t1 \'
   echo '  tmax=$t2 \'
   echo '  emin=5000 emax=500000 zmax=90'
   echo ''
   echo ''
   echo 'echo $x >> /data/ajm/SN/'$script'/myfile.out'
   echo ''
   echo 'gtmktime \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  filter="DATA_QUAL>0 && LAT_CONFIG==1" \'
   echo '  roicut=no evfile=/data/ajm/SN/'$script'/filtered_87A_EA_n$x.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/gti_87A_EA_n$x.fits'
   echo ''
   echo 'gtmktime \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  filter="DATA_QUAL>0 && LAT_CONFIG==1" \'
   echo '  roicut=no evfile=/data/ajm/SN/'$script'/filtered_87A_EB_n$x.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/gti_87A_EB_n$x.fits'
   echo ''
   echo 'gtmktime \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  filter="DATA_QUAL>0 && LAT_CONFIG==1" \'
   echo '  roicut=no evfile=/data/ajm/SN/'$script'/filtered_87A_EC_n$x.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/gti_87A_EC_n$x.fits'
   echo ''
   echo ''
   echo 'gtbin \'
   echo '  algorithm=HEALPIX \'
   echo '  evfile=/data/ajm/SN/'$script'/gti_87A_EA_n$x.fits \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/hpmap_87A_EA_n$x.fits \'
   echo '  hpx_ordering_scheme=RING hpx_order=9 coordsys=GAL hpx_ebin="NO" hpx_region=""'
   echo ''
   echo 'gtbin \'
   echo '  algorithm=HEALPIX \'
   echo '  evfile=/data/ajm/SN/'$script'/gti_87A_EB_n$x.fits \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/hpmap_87A_EB_n$x.fits \'
   echo '  hpx_ordering_scheme=RING hpx_order=9 coordsys=GAL hpx_ebin="NO" hpx_region=""'
   echo ''
   echo 'gtbin \'
   echo '  algorithm=HEALPIX \'
   echo '  evfile=/data/ajm/SN/'$script'/gti_87A_EC_n$x.fits \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/hpmap_87A_EC_n$x.fits \'
   echo '  hpx_ordering_scheme=RING hpx_order=9 coordsys=GAL hpx_ebin="NO" hpx_region=""'
   echo ''
   echo ''
   echo 'gtltcube \'
   echo '  evfile=/data/ajm/SN/'$script'/gti_87A_EA_n$x.fits \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/cub_87A_EA_n$x.fits \'
   echo '  zmax=90 dcostheta=0.025 binsz=1'
   echo ''
   echo 'gtltcube \'
   echo '  evfile=/data/ajm/SN/'$script'/gti_87A_EB_n$x.fits \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/cub_87A_EB_n$x.fits \'
   echo '  zmax=90 dcostheta=0.025 binsz=1'
   echo ''
   echo 'gtltcube \'
   echo '  evfile=/data/ajm/SN/'$script'/gti_87A_EC_n$x.fits \'
   echo '  scfile=/data/ajm/new_fermi_data/L200305052624F357373F06_SC00.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/cub_87A_EC_n$x.fits \'
   echo '  zmax=90 dcostheta=0.025 binsz=1'
   echo ''
   echo ''
   echo 'gtexpcube2 \'
   echo '  infile=/data/ajm/SN/'$script'/cub_87A_EA_n$x.fits \'
   echo '  cmap=/data/ajm/SN/'$script'/hpmap_87A_EA_n$x.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/ehp_87A_EA_n$x.fits \'
   echo '  irfs=P8R3_SOURCE_V2'
   echo ''
   echo 'gtexpcube2 \'
   echo '  infile=/data/ajm/SN/'$script'/cub_87A_EB_n$x.fits \'
   echo '  cmap=/data/ajm/SN/'$script'/hpmap_87A_EB_n$x.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/ehp_87A_EB_n$x.fits \'
   echo '  irfs=P8R3_SOURCE_V2'
   echo ''
   echo 'gtexpcube2 \'
   echo '  infile=/data/ajm/SN/'$script'/cub_87A_EC_n$x.fits \'
   echo '  cmap=/data/ajm/SN/'$script'/hpmap_87A_EC_n$x.fits \'
   echo '  outfile=/data/ajm/SN/'$script'/ehp_87A_EC_n$x.fits \'
   echo '  irfs=P8R3_SOURCE_V2'
   echo ''
   echo ''
   echo 'let t1=t1+604800'
   echo 'let t2=t2+604800'
   echo ''
   echo 'done'

} >> $script/1987A_$script.sh

done

# Make all the scripts executable
chmod 755 */1987A_*.sh


