#!/usr/bin/perl -w
# create_ima
# Creates image of XMM data using SAS.

print "\ncreate_ima started at\n";
system("date") && die;
print "\n";

$cluster = "A2204";
$det  = "PN";
$lowe	= "300";		# lower energy bound [eV]
$hie	= "10000";	        # upper energy bound [eV]
if($det eq "PN"){
    $EVE_DIR = "/aibn155_h/lab/dan1gast/$cluster/analysis/epchain";
    $curdir = 'pwd';
    chdir $EVE_DIR || die "Can't cd to $EVE_DIR\n";
    $curdir = 'pwd';
    print "The current directory is $curdir .\n";
    $in	    = "table=events.fits";
    $gti    = "&& GTI(lc_PN.fits,TIME)";
}elsif($det eq "MOS1"){
    $EVE_DIR = "/aibn155_h/lab/dan1gast/$cluster/analysis/emchain";
    $curdir  = 'pwd';
    chdir $EVE_DIR || die "Can't cd to $EVE_DIR\n";
    $curdir  = 'pwd'; 
    print "The current directory is $curdir .\n";
    $in	     = "table=events1.fits";
    $gti     = "&& GTI(lc_MOS1.fits,TIME)";
}elsif($det eq "MOS2"){
    $EVE_DIR = "/aibn155_h/lab/dan1gast/$cluster/analysis/emchain";
    $curdir  = 'pwd';
    chdir $EVE_DIR || die "Can't cd to $EVE_DIR\n";
    $curdir  = 'pwd';  
    print "The current directory is $curdir .\n";
    $in	     = "table=events2.fits";
    $gti     = "&& GTI(lc_MOS2.fits,TIME)";
} else {print "Invalid detector name $det .\n" && die;}

# --- Define global parameters for SAS command. ---
$coords	= "xcolumn=X ycolumn=Y";# Use Sky pixels.
#$coords= "xcolumn=DETX ycolumn=DETY";# Use detector pixels.
$bin	= "imagebinning=binSize ximagebinsize=82 yimagebinsize=82";
$sets	= "withimageset=true";
	
$vig	= "withzcolumn=Y withzerrorcolumn=N";	# Sum up (vignetting) weights instead of counts (created by add_vigweights.pl).
$expr	= "expression='PATTERN<=4 && FLAG==0 && PI>=$lowe && PI<=$hie $gti'";
$outf	= "i_$lowe\-$hie.fits";
$out	= "imageset=$outf";
$nice	= "/usr/bin/nice -n 0";	# Be nice.
$logfil_ima	= "create_ima.log";
#--------------------------------------------------


# --- Regulate amount of information put into log files. ---
# SAS_VERBOSITY ge 0; the larger the more information.
$ENV{"SAS_VERBOSITY"} = 5;
print "System variable SAS_VERBOSITY has been set to $ENV{'SAS_VERBOSITY'}.\n\n";
# ----------------------------------------------------------

# --- Set memory model to high. ---
$ENV{"SAS_MEMORY_MODEL"} = "high";
print "System variable SAS_MEMORY_MODEL has been set to $ENV{'SAS_MEMORY_MODEL'}.\n\n";
# ---------------------------------


# --- Set CIF file ---
$ENV{"SAS_CCF"} = "/aibn155_h/lab/dan1gast/$cluster/analysis/2005-3-1.cif";
print "System variable SAS_CCF has been set to $ENV{'SAS_CCF'}.\n\n";


# --- Do commands. ---
print "Creating image $outf.\n";
print "You may monitor the progress using, e.g., tail -f $logfil_ima...\n";
system("$nice evselect $in $coords $bin $expr $sets $vig $out >& $logfil_ima") &&
die "\nProblems creating an image.\nCheck the log file $logfil_ima. Died";
print "done.\n\n";
# --------------------


# Notes:
# pattern = 0: Only singles.
# flag = 0: Quality flag, using only highest quality (0) removes some columns.
# - To set the output data type explicitly use (by default evselect decides -
#   what to use) and don't forget to add it to the evselect task below:
#   $widt	= "withimagedatatype=true";
#   $idt	= "imagedatatype=Int32";	# Int8, Int16, Int32, Real32, Real64
