#! /bin/tcsh -f

echo "***********"
echo "*  Setup  *"
echo "***********"

setenv LANG "en_GB.UTF-8"
setenv LC_ALL "en_GB.UTF-8"
unset noclobber
date
set et = \&
set etm = \@
set escl = \!
set p1 = \(
set p2 = \)
set q1=\[
set q2=\]
set dol=\$
set vir=\"
set ap=\'
set interr=\?
set duep=\:
set m_in = \<
set po = \.

echo "ctrl_setup=yes" > ctrl_setup.txt

########################
## PLEASE MODIFY HERE ##
########################
set start_dir = /home/cloudadm/EXTRAS/D44_software_transient  
set software_dir = /Users/Gio/Software
set sas_dir = /cvmfs/extras-fp7.egi.eu/extras/xmmsas_20141104_1833

#AMBIENTE
#"mac" or "cluster" ambient
set ambient = "cluster"

#SETUP VERSION
#a numerical code to mark different analysis setups
set version_analysis = 01 

##TREE STRUCTURE
#name of the ascii file listing the observation IDs to be processed, which are present in the pps_archive_dir directory
set obs_list = obslist_torun.txt

#EVENT FILES BARYCENTR
#to set "yes" if event files on which the analysis is performed are barycentered 
###set barycentr = "yes"

	
##KIND OF ANALYSIS
#FULLDET
#"fulldet" or "no" to perform the fulldet approach, or not
set type_det = "no"
set ctrl_fulldet = 0

#DIFFERENT TIME BIN ANALYSIS
#BINFIXED
#"binfixed" or "no" to perform the binfixed approach, or not

###set type_anal_fixed = "no"
	

#BAYESIAN
#"bayesian" or "no" to perform the bayesian approach, or not
###set type_anal_bayesian = "bayesian"
	

#TOTAL EXPOSURE
set total_expo = "no"

#ODF
set ODF = "yes"

#########
#SET DIR#
#########

set prod_start_dir = $start_dir/WP4_v"$version_analysis"  
set result_dir = $start_dir/results
set archivio_dir = $start_dir/archive 
set data_dir = $start_dir/data
set log_dir_start = $start_dir/log
set bin_dir = $result_dir/BIN
set ref = $start_dir/ref_data
set progr_dir = $start_dir/WP4scripts
set xmmdac_dir = $progr_dir/WP4_PY/python/xtwp4/scripts/
set pps_archive_dir = $start_dir/PPS
set ros_asc_dir = $ref/ros_asc_files

if ( -e $pps_archive_dir ) then
   echo "OK PPS DATA ARCHIVE DIR"
else
   mkdir $pps_archive_dir/
   echo "============================================================"
   echo "FATAL ERROR: failed data directory"
   echo "missing data"
   echo "============================================================"
   break
endif

if ( -e $data_dir ) then
   echo "OK DATA ANALYSIS DIR"
else
   mkdir $data_dir/
endif

if ( -e $result_dir ) then
   echo "OK RESULTS DIR"
else
   mkdir $result_dir/
endif

#ENVIRONMENT
##SAS14.0.0
setenv SAS_DIR $sas_dir
setenv SAS_PATH $sas_dir
source $SAS_PATH/sas-setup.csh

#SAS CCF
setenv SAS_CCFPATH $sas_dir/ccf
setenv SAS_MEMORY_MODEL high

#PYTHON
setenv PATH "$software_dir/Anaconda/bin:$PATH"

#XMMDAC
setenv WP4INST $progr_dir/WP4_PY
setenv PATH ${PATH}:${WP4INST}/python/xtwp4/scripts
setenv PYTHONPATH ${PYTHONPATH}:${WP4INST}/python

#HEADAS
setenv HEADASNOQUERY
setenv HEADASPROMPT /dev/null

###########
#PARAMETER#
###########

#ANALISYS
#INSTRUMENTS
# DET: 
#numerical code to select the instrument event files from which the time bins are constructed
# 1  MOS1+MOS2 
# 2  MOS1+MOS2+pn
# 01 MOS1
# 02 MOS2
# 03 pn

###set DET = 2
	
#numerical code to select the instrument event files on which the source detection is performed
set DET_SRC = ${DET}

#to select and to use the MOS Prime Partial event file 
set primepartial = "no"

if ($DET_SRC == 01 || $DET_SRC == 02 || $DET_SRC == 03) then
 set single_instrument = "yes"
else
 set single_instrument = "no"
endif

#SOFT PROTONS
#       filter0 - fulldet
#"SP" or "notSP" to perform the source detection of the whole observation on proton flare cleaned event files, or not
###set filter0 = "notSP"

	
#       filter1 - bin
#"SP" or "notSP" to perform the source detection of the time intervals on proton flare cleaned event files, or not
set filter1 = ${filter0}

#BINFIXED PARAMETERS: TIME BINS
# time step to select the time bin intervals in the binfixed approach (for example -in sec-: 1000)
###set deltat = 1000
	

#BAYESIAN PARAMETERS
#close to 3XMM sources bayesian analysis

###set closetosrc = "no"

	
#outside to 3XMM sources bayesian analysis
if ($type_anal_bayesian == "bayesian") then
 set outtosrc = "yes"
else
 set outtosrc = "no"
endif
#if the bayesian analysis is performed in the total energy range 0.2-12 keV set 1, 0 if not
set bayesian_band = 1

#3XMM FLAG
#pn bayesian flag: 0 if the filter is with only the FLAG=0, 1 if the filter is with the complete
set flag_bayesian = 1

#maximum number of bayesian time bins  
set baye_n_bin_max = 100 
#info o debug
set info = "debug"
set ncpu = 2

#xtdac: OUT TO SRC 
#maximum time extension to keep a bayesian time bin (for example -in sec-: 3600, the time bins grater than 3600 sec are rejected)
#outtosrc
###set bayesian_max1 = 5000


#probability threshold of the bayesian block analysis (for example: 1e-04)

###set Ierror = 1e-04

	
#to annul the Ierror, set Ierror = 1
#if the threshold for final significance (stss_out) is <= 0, no likelihood analysis will be performed

###set stss_out = 5.0

	
###set regionsize = 30

	
set multiplicity = 3
set writeReg = "yes"

#xtfbs: CLOSE TO SRC 
#closetosrc
###set bayesian_max2 = 5000 

	
#Size of the fixed time bin
###set sftb = 1000


#Significance threshold for the search, in sigma
###set stss = 5.0

	
#Minimum size (in arcsec) for the regions to be considered for the search
set msz = 15

#search_var.py parameters
set trans = 0
set var = 4
set rat = 10

###set ext_threshold = 0   #Es. 6"*0.25 = 1.5 limite di 3XMM, 12" (3) limite di WP2

	
#CLEAN
#to remove the results/obs_id/detection and results/obs_id/processed directories set "yes", "no" if not 
set ctrl_clean = "yes"

#CROSSCHECK CATALOGUES
###set sist_err = 2


###set sig = 3


#WP2
#"yes" or "no" to compare the catalogue of sources detected in a time bin with the PPS catalogue without sources (scts < 100 in the energy band 0.2-12 keV) not considered by WP2, or not
###set cts_limit = 0

	
#BIN FIXED ANALYSIS
set bin = 0
# "full" or "no" to compare the catalogue of sources detected in a fixed time bin with the full catalogue, produced by the pipeline itself, or not
set type_bin_cat1 = "no"
#"3xmm" or "no" to compare the catalogue of sources detected in a fixed time bin with the PPS catalogue, or not
set type_bin_cat2 = "3xmm"

#BAYESIAN ANALYSIS
set baye = 0 
#"full" or "no" to compare the catalogue of sources detected in a bayesian time bin with the full catalogue, produced by the pipeline itself, or not
set type_baye_cat1 = "no"
# "3xmm" or "no" in order to compare the catalogue of sources detected in a bayesian time bin with the PPS catalogue, or not
set type_baye_cat2 = "3xmm"

#out: to filter the PPS catalogue sources with a variable radius 
#region: to filter the PPS catalogue sources with optimised ellisoidal box. The region task is used (to set bkgfraction parameter) 
#in:  to filter the regions outside of a circle with variable radius around the PPS catalogue sources  
#no:  to filter the PPS catalogue and fulldet catalogue sources with a fixed radius 
set region_cheese = "region"
set bkgfraction = 1

#to set with region_cheese = "no" 
set fixed_radius = 10
#spatial error in arcesec
set region_sys = 5

if ( $type_bin_cat2 == "no" && $type_baye_cat2 == "no" ) then
 set band_analysis = 0 
else
 if ( $type_bin_cat2 == "3xmm" && $type_baye_cat2 == "3xmm" ) then
  set band_analysis = 1
 else
  echo "FATAL ERROR: BIN $type_bin_cat2 and BAYESIAN $type_baye_cat2 are not equal"
 endif
endif

#ENERGY BANDS
#energy bands for the analysis without the 3XMM catalogue check 
if ($band_analysis == 0) then
# set b1 = "300;10000"
 set b1 = "200;12000"
 set b2 = "200;500"
 set b3 = "500;1000"
 set b4 = "1000;2000"
 set b5 = "2000;4500"
 set b6 = "4500;12000"

# set d1 = "0.3;10."
 set d1 = "0.2;12."
 set d2 = "0.2;0.5"
 set d3 = "0.5;1."
 set d4 = "1.;2."
 set d5 = "2.;4.5"
 set d6 = "4.5;12."

 set band_ref=`echo ${b1}/${b2}/${b3}/${b4}/${b5}/${b6}` 
endif

#energy bands for the analysis with the 3XMM catalogue check 
if ( $band_analysis == 1) then
# set b1 = "300;10000"
 set b1 = "200;12000"
 set b2 = "200;500"
 set b3 = "500;1000"
 set b4 = "1000;2000"
 set b5 = "2000;4500"
 set b6 = "4500;12000"

# set d1 = "0.3;10."
 set d1 = "0.2;12."
 set d2 = "0.2;0.5"
 set d3 = "0.5;1."
 set d4 = "1.;2."
 set d5 = "2.;4.5"
 set d6 = "4.5;12."

 set band_ref=`echo ${b1}/${b2}/${b3}/${b4}/${b5}/${b6}` 
endif

#energy bands in which search the bayesian time bins
 set ba1 = "200;12000"
 set ba2 = "500;2000"
 set ba3 = "2000;10000"

#TARGET SRC

#transient 01
set target_src = "no"
set target_name = "TRANSIENT01"
set target_RA = 0 #RA 
set target_DEC = 0 #DEC
set max_dist = 8

#SPECTRA OF TARGET SRC
set target_spectra = "no"
set bmin_spec = 100
set bmax_spec = 14000
set radius_spec_src = 400
set radius_spec_bkg = 1200
set grppha_bin = 10 
set spec_baryecentered = "no"
set bkg_spec_pos = "yes"
if ($bkg_spec_pos == "yes") then
 set bkg_RA = 271.51175
 set bkg_DEC = -27.783118
endif

#numerical code to mark the total energy band
set b_tot = 1 
#numerical code to mark the softer energy band
set b_soft = 2 
#numerical code to mark the exposure map in the total energy band
set b_expo = 1 

#numerical code to mark the energy band that include the Cu energy range 7.8-8.2 keV in order to clean the Cu cts of the pn instrument
#to set 0 in order to not clean the Cu cts of the pn instrument
set bCU = 0 

#HR parameter: in eboxdetect and in emldetect tasks (max 6)
if ( $band_analysis == 1) then
 set HR = "1 2 2 3 3 4 4 5"
endif
if ( $band_analysis == 0) then
 set HR = "2 3"
endif

#SPECTRA
#files in ref_data/ directory to calculate the CF
#MOS1 medium
set spect1 = `echo $ref/source_m1.pi`
set resp1 = `echo $ref/source_m1.rmf`
set arf1 = `echo $ref/source_m1.arf`
set backg1 = `echo $ref/back_m1.pi`
#MOS2 medium
set spect2 = `echo $ref/source_m2_new.pi`
set resp2 = `echo $ref/source_m2.rmf`
set arf2 = `echo $ref/source_m2_new.arf`
set backg2 = `echo $ref/back_m2.pi`
#PN thin
set spect3 = `echo $ref/spectrum_1E0056_ellipse_30.pi`
set resp3 = `echo $ref/spectrum_1E0056_ellipse.rmf`
set arf3 = `echo $ref/spectrum_1E0056_ellipse.arf`
set backg3 = `echo $ref/spectrum_back_ellipse.pi`

#to use a fixed Nh for all observation set 1, 0 if not
set model_fix=0 
set spi=1.7
#spectral model
set model_spect = `echo wabs"$p1"pow"$p2" "$et" 0.058 "$et" "$spi" "$et" 1 `

#in order to calculate the CF for logN-logS
#set model_spect = `echo wabs"$p1"pow"$p2" "$et" 0 -1 "$et" 1.7 "$et" 1 `

echo "Start to check the parameters"

################################
#PARAMETER PRELIMINARY ANALYSIS#
################################

#emask parameters
set mask_threshold1 = 0.5
set mask_threshold2 = 1
set mask_threshold1b = 0.5
set mask_threshold2b = 1

#source detection in local mode parameters: eboxdetect_LM
set likemin_l = 5 #11 #15  #3 #8tiengo  ##1-50 #eng 5
set likemin_lb = 5 #11 #13  #3 #8tiengo
set boxsize_l = 5 #4 last  #3 #8 #eng 5

#source detection in local mode parameters: esplinemap_LM
set mlmin_l = 8   #30 #10 #1  ##0-50 #eng default 
set mlmin_lb = 8   #30 #10 #1 
set scut_l = 0.002  #0.05 #0.01 #eng 0.002
set nsplinenodes_l = 13  #16 #eng 13 #6
set fitmethod_l = "spline" #eng spline 
set nfitrun_l = 4 #eng 4 

#source detection in map mode parametres: eboxdetect_MM
set likemin_m = 5 #40 #45 #5 #8tiengo  ##1-50 #eng 5
set likemin_mb = 5 #40 #45 #5 #8tiengo
set boxsize_m = 5  #3 #8 #eng 5

##############################
#STANDARD ANALYSIS PARAMETRES#
##############################
#source detection in map mode parameters: emldetect
set mlmin_m = 6 #30 #6 #29 #30 in 1207 #10tiengo   ##1-50 #eng 6
set mlmin_mb = 6 #6 #29 #30 in 1207
set scut_m = 0.9 #0.9 in 1207
set ecut_m = 15 #0.68 #0.68 in 1207 #eng 15
set nmaxfit_m = 1
set dmlextmin = 4
set fitextent = "yes"
set extentmodel = "beta"
set psfmodel = "ellbeta"
set nmulsou = 2
set threshold = 10
#likelihood: sources with DET_ML > $det_ml are selected
#DET_ML=-ln(P), DET_ML=8.5 => P=2E-04
#if not sources selection: det_ml=0
set det_ml = 0
set det_mlb = 0

#PRODUCTS
#PLOT
#to save images final PNG set plot0 to 1, 0 if not
#to save images PNG set plot1 to 1, 0 if not
#to save images RGB set plot2 to 1, 0 if not
set plot0 = 1 
set plot1 = 0 
set plot2 = 0
#to save fulldet catalogues for each energy band and instrument 
#set full_list to yes, no if not
set full_list = "no"
#to save time bin catalogues for each energy band and instrument 
#set bin_list to yes, no if not
set bin_list = "no"

#STAT IMAGE
#to save the statistic informations of the sky images set stat_img to 1, 0 if not
set stat_img = 0

#MOS EXPO STANDARD
set mos_expo_st = "no"

#SENSIBILITY MAP
#to save the sensitivity map set sensitivity to yes, no if not
set sensitivity = "yes"

#PILEUP MASK
#to save the pileup mask set pileup to yes, no if not
set pileup = "no"

#MOSAIC IMAGE
#to save the mosaiked maps (sky images, exposure maps and background maps) set emosaic to yes, no if not
set emosaic = "yes"

#SAS VERBOSITY
set verb = 1

#SAS WARNING
set warn = 0

set N_OBS=`ls $pps_archive_dir | wc -l | awk '{print $1}'`
if ( $N_OBS != 0) then
 set N_tot = $N_OBS
 set N_OBS_ANAL=`cat $start_dir/$obs_list | wc -l | awk '{print $1}'`
 set N_OBS_PERC=`echo $N_OBS_ANAL $N_tot | awk '{printf("%.3f \n",$1/$2)}'`
else
 echo "ERROR: no observations in $pps_archive_dir"
endif

###########
#CHECK DIR#
###########
if ( -r $log_dir_start ) then
   echo "OK LOG DIR"
else
   echo "MAKE LOG DIR"
   mkdir $log_dir_start/
endif

if ( -e $ref ) then
   echo "OK REF DATA DIR"
else
   mkdir $ref/
   echo "============================================================"
   echo "FATAL ERROR: failed $ref directory"
   echo "missing data"
   echo "============================================================"
   break
endif

if ( -e $archivio_dir ) then
   echo "OK ARCHIVE DIR"
else
   echo "MAKE ARCHIVE DIR"
   mkdir $archivio_dir/
endif

if ( -e $progr_dir ) then
   echo "OK ANALYSIS DIR"
else
   mkdir $progr_dir/
   echo "============================================================"
   echo "FATAL ERROR: failed analysis directory"
   echo "missing programs"
   echo "============================================================"
   break
endif

#########
#MESSAGE#
#########

echo "===================================================================================="
echo " ATTENTION:                      source detection: $type_det                       "
echo " ATTENTION:                 bin bayesian analysis: $type_anal_bayesian                       "
echo " ATTENTION:                bin fixed analysis: $type_anal_fixed                       "
echo " ATTENTION:                spectral analysis on target $target_name : $target_spectra                       "
if ( $type_anal_bayesian == "bayesian") then
#echo "                             bayesian parameters: acd="$acd"  maxd="$maxd"    "
endif
if ( $type_anal_bayesian == "bayesian") then
 if ( $type_baye_cat1 == "full") then
  echo " ATTENTION: (fulldet) bayesian bin catalogs cross-checked with $type_baye_cat1 catalog"
 endif
 if ( $type_baye_cat2 == "3xmm") then
  echo " ATTENTION: (3XMM) bayesian bin catalogs cross-checked with $type_baye_cat2 catalog"
 endif
endif
if ( $type_anal_fixed == "binfixed") then
echo " ATTENTION:                 time bin fixed (sec): $deltat                           "
endif
if ( $type_anal_fixed == "binfixed") then
 if ( $type_bin_cat1 == "full") then
  echo " ATTENTION: (fulldet) fixed bin catalogs cross-checked with $type_bin_cat1 catalog"
 endif
 if ( $type_bin_cat2 == "3xmm") then
  echo " ATTENTION: (3XMM) fixed bin catalogs cross-checked with $type_bin_cat2 catalog"
 endif
endif
echo " ATTENTION:   searching var parameters in search_var.py: trans=$trans var=$var ext_threshold=$ext_threshold          "
echo " ATTENTION:                  make image, expo: with event file $filter0 cleaned      "
if ( $DET == 1) then
echo "                                   instrument: EPIC MOS1 + MOS2                           "
endif
if ( $DET == 01) then
echo "                                   instrument: EPIC MOS1                                    "
endif
if ( $DET == 02) then
echo "                                   instrument: EPIC MOS2                                  "
endif
if ( $DET == 03) then
echo "                                   instrument: EPIC pn                                    "
endif
if ( $DET == 2) then
echo "                                   instrument: EPIC MOS1 + MOS2 + pn                      "
endif
if ($target_src == "yes") then
 echo "                             target source: $target_name                                                             "
 echo "                          target source RA: $target_RA                                                             "
 echo "                         target source Dec: $target_DEC                                                             "
endif
if ( ` echo $band_ref | wc -w ` > 0 ) then
   echo "               energy bands selected are (eV): $band_ref                            "
else
   echo "ERROR: missing the energy bands"
endif
if ( ` echo $model_spect | wc -w ` > 0 ) then
   echo "            spectral model selected for CF is: $model_spect                         "
else
   echo "ERROR: missing the spectral model for CF"
endif
if ( ` echo $model_fix | wc -w ` > 0 ) then
   if ($model_fix == 0) then
     echo "     Nh not fixed in the CF calculate      : $model_fix             "
   else
     echo "         Nh fixed in the CF calculate      : $model_fix              "
   endif
else
   echo "ERROR: missing the model to calculate CF  "
endif


if ( ` echo $N_tot | wc -w ` > 0 ) then
   echo "                       number of observations in $pps_archive_dir : $N_tot                               "
   echo "                       number of observations to be analysed are: "$N_OBS_ANAL"/"$N_tot" ("$N_OBS_PERC")     "
else
   echo "ERROR: missing the total number of observations"
endif

echo "===================================================================================="
echo " PARAMETERS:                                                                        "
     echo " PRELIMINARY SOURCE DETECTION                                                                        "
     if ( ` echo $likemin_l | wc -w ` > 0 ) then
       echo "              likemin (eboxdetect local mode): $likemin_l                           "
       echo "              likemin (eboxdetect local mode bin): $likemin_lb                             "
     else
       echo "ERROR: missing the likemin param (eboxdetect local mode) "
     endif
     if ( ` echo $boxsize_l | wc -w ` > 0 ) then
       echo "              boxsize (eboxdetect local mode): $boxsize_l                           "
     else
       echo "ERROR: missing the boxsize param (eboxdetect local mode) "
     endif
     if ( ` echo $mlmin_l | wc -w ` > 0 ) then
       echo "                mlmin (esplinemap local mode): $mlmin_l                             "
       echo "                mlmin (esplinemap local mode bin): $mlmin_lb                             "
     else
       echo "ERROR: missing the mlmin param (esplinemap local mode) "
     endif
     if ( ` echo $scut_l | wc -w ` > 0 ) then
        echo "                scut (esplinemap local mode): $scut_l                              "
     else
        echo "ERROR: missing the scut param (esplinemap local mode) "
     endif
     if ( ` echo $nsplinenodes_l | wc -w ` > 0 ) then
        echo "                nsplinenodes (esplinemap local mode): $nsplinenodes_l                              "
     else
        echo "ERROR: missing the nsplinenodes param (esplinemap local mode) "
     endif
     if ( ` echo $fitmethod_l | wc -w ` > 0 ) then
        echo "                fitmethod (esplinemap local mode): $fitmethod_l                              "
     else
        echo "ERROR: missing the fitmethod param (esplinemap local mode) "
     endif
     if ( ` echo $likemin_m | wc -w ` > 0 ) then
        echo "               likemin (eboxdetect map mode): $likemin_m                           "
        echo "               likemin (eboxdetect map mode bin): $likemin_mb                             "
     else
        echo "ERROR: missing the likemin param (eboxdetect map mode) "
     endif
     if ( ` echo $boxsize_m | wc -w ` > 0 ) then
       echo "                boxsize (eboxdetect map mode): $boxsize_m                           "
     else
       echo "ERROR: missing the boxsize param (eboxdetect map mode) "
     endif
     echo " FINAL SOURCE DETECTION: EMLDETECT                                                                        "
     if ( ` echo $mlmin_m | wc -w ` > 0 ) then
        echo "                           mlmin (emldetect): $mlmin_m                             "
        echo "                           mlmin (emldetect bin): $mlmin_mb                             "
     else
        echo "ERROR: missing the mlmin param (emldetect) "
     endif
     if ( ` echo $scut_m | wc -w ` > 0 ) then
        echo "                            scut (emldetect): $scut_m                              "
     else
        echo "ERROR: missing the scut param (emldetect) "
     endif
     if ( ` echo $dmlextmin | wc -w ` > 0 ) then
        echo "                            dmlextmin (emldetect): $dmlextmin                              "
     else
        echo "ERROR: missing the scut param (emldetect) "
     endif
     if ( ` echo $nmulsou | wc -w ` > 0 ) then
        echo "                            nmulsou (emldetect): $nmulsou                              "
     else
        echo "ERROR: missing the scut param (emldetect) "
     endif
     if ( ` echo $threshold | wc -w ` > 0 ) then
        echo "                            threshold (emldetect): $threshold                              "
     else
        echo "ERROR: missing the scut param (emldetect) "
     endif
     if ( ` echo $extentmodel | wc -w ` > 0 ) then
        echo "                            extentmodel (emldetect): $extentmodel                              "
     else
        echo "ERROR: missing the scut param (emldetect) "
     endif
     if ( ` echo $psfmodel | wc -w ` > 0 ) then
        echo "                            psfmodel (emldetect): $psfmodel                              "
     else
        echo "ERROR: missing the scut param (emldetect) "
     endif
     if ( ` echo $ecut_m | wc -w ` > 0 ) then
        echo "                            ecut (emldetect): $ecut_m                              "
     else
        echo "ERROR: missing the ecut param (emldetect) "
     endif
     if ( ` echo $nmaxfit_m | wc -w ` > 0 ) then
        echo "                         nmaxfit (emldetect): $nmaxfit_m                           "
     else
        echo "ERROR: missing the nmaxfit param (emldetect) "
     endif
     if ( ` echo $det_ml | wc -w ` > 0 ) then
        echo "                 det_ml (selection det_ml=-ln(P) creabkg): $det_ml                           "
     else
        echo "ERROR: missing the det_ml param (selection det_ml=-ln(P) creabkg) "
     endif
echo "===================================================================================="

echo ""
echo ""
echo ""
echo "CHECK WP4 PRODUCTS AND SETUP VERSION================================================="

#WP4PRODUCTS
if ( -r $prod_start_dir ) then
 echo ""$prod_start_dir" already exists"
 if ( -e $prod_start_dir/setup.csh) then
  
  echo ""
  set setup_diff=`diff $progr_dir/setup.csh $prod_start_dir/setup.csh`
  set ctrl_diff=`echo $setup_diff | awk '{print $2}'`
  if ($ctrl_diff == $m_in) then
   echo "WARNING: please check $progr_dir/setup.csh $prod_start_dir/setup.csh DIFFERENCIES"
   diff $progr_dir/setup.csh $prod_start_dir/setup.csh
  else
   echo "OK: $progr_dir/setup.csh $prod_start_dir/setup.csh are not different"
  endif
  echo ""
 endif
else
 mkdir $prod_start_dir
 echo "WP4 final results directory "$prod_start_dir" done"
 cp $progr_dir/setup.csh $prod_start_dir
endif
echo "====================================================================================="
echo ""
echo ""
echo ""
