#!/bin/csh
# Based on a script by Bruce O'Neel and adapted by Ada Paizis
# Ada.Paizis@obs.unige.ch
#
# PARAMETER
#
# par1: scwid without extension       e.g. "00520010010"
# before launching the script create a directory where you
# want to put your "database" and change to this dierectory

### SETUP THE ENVIRONMENT FOR OSA (MODIFY THIS LINE ACCORDING TO YOUR LOCAL 
#   SETTINGS

setenv REP_BASE_PROD  $PWD				
setenv COMMONSCRIPT 1
setenv COMMONLOGFILE +log.$1

### CREATE THE OBSERVATION GROUP named by SCW ID
setenv REVNO `echo $1 | cut -b1-4`
setenv OGID $1
echo "scw/"$REVNO/$OGID".001/swg.fits[1]" > $HOME/$$.lis.ws
cat $HOME/$$.lis.ws
og_create idxSwg=$HOME/$$.lis.ws \
ogid=$OGID baseDir="./" instrument=IBIS obs_id=""
rm -f $HOME/$$.lis.ws
rm -f $REP_BASE_PROD/log.$OGID


### RUN IBIS OSA TILL IMA STEP. You can choose the parameters, like the
# energy bands, suitable to your analysis

cd $REP_BASE_PROD/obs/$OGID

nice ibis_science_analysis ogDOL="./og_ibis.fits[1]" \
SWITCH_disablePICsIT=yes  \
CAT_refCat="$ISDC_REF_CAT[ISGRI_FLAG==1]" \
startLevel="COR"\
endLevel="IMA"\
IBIS_II_ChanNum=3\
IBIS_II_E_band_min="17 40 100"\
IBIS_II_E_band_max="40 100 300"\
OBS1_SearchMode=3\
OBS1_MinCatSouSnr=3\
OBS1_MinNewSouSnr=6\
OBS1_ToSearch=10\
OBS1_DoPart2=1

### CLEAN AND GZIP IMA FILES TO SAVE SPACE

fdelrow og_ibis.fits+1 2 4 N Y
rm -f ./isgri_mosa_ima.fits
rm -f ./isgri_mosa_res.fits
rm -f ./isgri_catalog.fits
rm -f ./GNRL-REFR-CAT.fits
gzip  ./isgri_srcl_res.fits
gzip ./scw/$OGID.001/isgri_sky_res.fits
gzip ./scw/$OGID.001/isgri_sky_ima.fits

### CHECK IF THERE ARE SOURCES IN THE IMA STEP. 
#   IF NOT CLEAN, GZIP AND EXIT
#   IF YES LAUNCH SPE, LCR AND THEN CLEAN, GZIP

 if ( `dal_list scw/$OGID.001/isgri_sky_res.fits.gz+2 | grep Rows | awk '{print $NF}'` == 0 ) then

	dal_detach object="./scw/$OGID.001/swg_ibis.fits[1]" child="./scw/$OGID.001/compton_dead.fits[1]" delete=y
	gzip swg_idx_ibis.fits
	gzip og_ibis.fits
	gzip log.$OGID
	gzip ./scw/$OGID.001/ibis_gti.fits
	gzip ./scw/$OGID.001/isgri_cor_shad.fits
	gzip ./scw/$OGID.001/isgri_detector_shadowgram.fits
	gzip ./scw/$OGID.001/isgri_efficiency_shadowgram.fits
	gzip ./scw/$OGID.001/swg_ibis.fits
	gzip ./scw/$OGID.001/isgri_cor_events.fits
	gzip ./scw/$OGID.001/isgri_dead.fits
        exit
 else
 
	### PREPARE CATALOGUE FOR SPE AND LCR
	#

	fcopy "isgri_srcl_res.fits.gz[ISGR-SRCL-RES][DETSIG >= 3]" specat.fits
	chmod -w specat.fits

	### LAUNCH SPE, LCR
	# (IBIS_SI_inEnergyValues)

	nice ibis_science_analysis ogDOL="./og_ibis.fits[1]" \
	SWITCH_disablePICsIT=yes  \
	CAT_refCat="$ISDC_REF_CAT[ISGRI_FLAG==1]" \
	startLevel="BIN_S" \
	endLevel="LCR" \
	SCW2_cat_for_extract="./specat.fits[1]" \
	ILCR_num_e=1 ILCR_e_min="17" ILCR_e_max="40" \
	ILCR_delta_t=300


	### CLEAN USELESS SPE-LCR FILES TO SAVE SPACE
	#
	#detach ISGR-SRCL-RES from the swg_ibis.fits. This points to specat.fits
	#   and was needed/created for the SPE/LCR levels but it prevents from
	#   mosaicking in the end. Since we do not need it anymore we detach it.

	dal_detach object="./scw/$OGID.001/swg_ibis.fits[1]" pattern=ISGR-SRCL-RES delete=n
	dal_detach object="./scw/$OGID.001/swg_ibis.fits[1]" child="./scw/$OGID.001/compton_dead.fits[1]" delete=y   

	rm -f ./rebinned_back_lc.fits
	rm -f ./rebinned_corr_lc.fits

	gzip ./specat.fits
	gzip swg_idx_ibis.fits
	gzip og_ibis.fits
	gzip log.$OGID
	gzip ./scw/$OGID.001/ibis_gti.fits
	gzip ./scw/$OGID.001/isgri_cor_shad.fits
	gzip ./scw/$OGID.001/isgri_detector_shadowgram.fits
	gzip ./scw/$OGID.001/isgri_efficiency_shadowgram.fits
	gzip ./scw/$OGID.001/isgri_pif.fits
	gzip ./scw/$OGID.001/swg_ibis.fits
	gzip ./scw/$OGID.001/isgri_lcr.fits
	gzip ./scw/$OGID.001/isgri_cor_events.fits
	gzip ./scw/$OGID.001/isgri_dead.fits
	gzip ./scw/$OGID.001/isgri_spectrum.fits


 endif


cd $REP_BASE_PROD

