#!/usr/local/bin/perl -w
# create_spec
# Creates spectrum of XMM data using SAS.
# Note that images may be created with the same command, just adding parameters.
# This may be useful for easy checking if spectral regions are really where one
# wants them to be.

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

$cluster = "A2204";
$det     = "PN";
$outf	 = "spec\_$det\_1.fits";
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)";
    $bin    = "spectralbinsize=5";	# pn
    $srg    = "specchannelmin=0 specchannelmax=20479";	# pn
}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)";
    $bin     = "spectralbinsize=15";	# mos
    $srg     = "specchannelmin=0 specchannelmax=11999";	# MOS
}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)";
    $bin     = "spectralbinsize=15";	# mos
    $srg     = "specchannelmin=0 specchannelmax=11999";	# MOS
} else {print "Invalid detector name $det .\n" && die;}


# --- Define global parameters for SAS command. ---

$sets	= "withspectrumset=true";
#$wds	= "writedss=false";
$wsr	= "withspecranges=true";
$egc	= "energycolumn=PI";
$vig	= "withzcolumn=Y withzerrorcolumn=N";	# Sum up (vignetting) weights instead of counts (created by add_vigweights.pl).
$out	= "spectrumset=$outf";
$expr	= "expression='annulus(26573.1106900462,27402.9420975201,300,900,X,Y) && PATTERN<=4 && FLAG==0 $gti'"; # pn
$nice	= "/usr/bin/nice -n 0";	# Be nice.
$logfil_spec	= "create_spec.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 spectrum $outf.\n";
print "You may monitor the progress using, e.g., tail -f $logfil_spec...\n";
system("$nice evselect $in $expr $sets $wsr $srg $bin $egc $vig $out >& $logfil_spec") &&
die "\nProblems creating a spectrum.\nCheck the log file $logfil_spec. Died";
print "done.\n\n";
# --------------------


print "create_spec ended at\n";
system("date") && die;
print "\nNow you may inspect the log file for warnings or errors:\n";
print "(*) $logfil_spec\n";
print "\n";
