#! /bin/sh
# This is the LHEA perl script: /cvmfs/extras-fp7.egi.eu/extras/heasoft/ftools/x86_64-unknown-linux-gnu-libc2.19-0/bin/fmosaic2img
# The purpose of this special block is to make this script work with
# the user's local perl, regardless of where that perl is installed.
# The variable LHEAPERL is set by the initialization script to
# point to the local perl installation.
#-------------------------------------------------------------------------------
eval '
if [ "x$LHEAPERL" = x ]; then
  echo "Please run standard LHEA initialization before attempting to run /cvmfs/extras-fp7.egi.eu/extras/heasoft/ftools/x86_64-unknown-linux-gnu-libc2.19-0/bin/fmosaic2img."
  exit 3
elif [ "$LHEAPERL" = noperl ]; then
  echo "During LHEA initialization, no acceptable version of Perl was found."
  echo "Cannot execute script /cvmfs/extras-fp7.egi.eu/extras/heasoft/ftools/x86_64-unknown-linux-gnu-libc2.19-0/bin/fmosaic2img."
  exit 3
elif [ `$LHEAPERL -v < /dev/null 2> /dev/null | grep -ic "perl"` -eq 0 ]; then
  echo "LHEAPERL variable does not point to a usable perl."
  exit 3
else
  # Force Perl into 32-bit mode (to match the binaries) if necessary:
  if [ "x$HD_BUILD_ARCH_32_BIT" = xyes ]; then
    if [ `$LHEAPERL -V 2> /dev/null | grep -ic "USE_64_BIT"` -ne 0 ]; then
      VERSIONER_PERL_PREFER_32_BIT=yes
      export VERSIONER_PERL_PREFER_32_BIT
    fi
  fi
  exec $LHEAPERL -x $0 ${1+"$@"}
fi
'
if(0);
# Do not delete anything above this comment from an installed LHEA script!
#-------------------------------------------------------------------------------
#!/usr1/local/bin/perl -w  

use Getopt::Std;

$opt_s=1;
$opt_o='outfile.fits';
$opt_c=10;
$opt_m=1;
$opt_h="no_help";

# Get the options on the command line.
if (!getopts('s:o:m:c:h:') || !$ARGV[0]) {
	errors();
}

if($opt_h ne "no_help") {
	errors();
}

$file_id =$ARGV[0];

# Print the configuation of work
print "Working configuration for this program:\n";
print "We will smooth the image, background, and exposure files with\n";

if( $opt_m == 1 ) {
	print "a gaussian of sigma $opt_m and put the result of the \n";
	print " [(Image-Background)/Exposure] in file $opt_o\n";
	print " Exposure map is cut below $opt_c % of the total exposure\n";
#	
	print "Smoothing the 3 image files\n";
	$_ = $file_id."_image.fits";
	system "fgauss $_ file1.fits $opt_s";
	$_ = $file_id."_back.fits";
	system  "fgauss $_ file2.fits $opt_s";
	$_ = $file_id."_emap.fits";
	system "fgauss $_ file3.fits $opt_s";
} 
else {
	print "an adaptive smoothing. There should be $opt_s count(s) under\n";
	print "the elliptical top hat filter for the smoothing to start. \n";
	print "The result of the [(Image-Background)/Exposure] is in file $opt_o\n";
	print "Exposure map is cut below $opt_c % of the total exposure\n";
#
	print "Smoothing the 3 image files\n";
	print "fadapt $file_id","_image.fits $opt_s\n";
	$_ = $file_id."_image.fits";
	system "fadapt $_ file1.fits $opt_s";
	$_ = $file_id."_back.fits";
	system "fadapt $_ file2.fits $opt_s";
	$_ = $file_id."_emap.fits";
	system "fadapt $_ file3.fits $opt_s";
}

	system "farith file1.fits file2.fits file12.fits -";
	print "Cut exposure map\n";
	system "fimgstat file3.fits threshlo=INDEF threshup=INDEF outfile=list4";

	open (FD,"list4") || die "can't open list4";
	while (<FD>) {
		chomp();
		if (/maximum/ && /image/) {
		@field =split(/\s+/);
		$max = $field[6]*$opt_c*0.01;
		}
	} 

	print "minimun for exposure map is $max\n";
	system "fimgtrim file3.fits threshlo=$max threshup=I const_lo=0 outfile =file5.fits";
	system "fimgtrim file12.fits threshlo=0 threshup=I const_lo=0 outfile=file12p.fits";
	print "Create final image\n";
	system "farith file12p.fits file5.fits $opt_o / blank=0 ";
	system "rm file1.fits file2.fits file12p.fits file12.fits file3.fits file5.fits list4";
	print "Done\n";

sub errors {
print "\n";
print " Usage: fmosaic2img -o <out_filename> <input_filename_root>\n";
print "\n";
print " This scripts allows you to combine the output files of fmosaic into\n";
print " a single background-subtracted, exposure-corrected image\n";
print " (Image-back)/exposrue (with smoothing and cut on exposure map).\n";
print " When you have produced image, background, and exposure maps using fmosaic,\n";
print " they will have the names of the form:\n";
print "\n";
print " name_005_040_image.fits or name_image.fits (if no energy cut is performed)\n";
print " name_005_040_back.fits or name_back.fits (if no energy cut is performed)\n";
print " name_005_040_emap.fits or name_emap.fits (if no energy cut is performed)\n";
print "\n";
print " where 'name' is the first parameter of fmosaic and the numbers represent\n";
print " the energy cut you made.  The same 'name' should be typed as the argument\n";
print " to fmosaic2img.\n";
print "\n";
print " Options are (also displayed by typing 'fmosaic2img' without any arguments):\n";
print " Usage: fmosaic2img [-s -o -m -c] input_file_root\n";
print "       -m specifies the method: 1 is for a gaussian (default)\n";
print "                                2 is for adaptive smoothing\n";
print "       -s specifies the sigma (gaussian) or \n";
print "          the number of counts required to smooth with fadapt (adapt)\n";
print "       -o specifies the output file name\n";
print "       -c specifies the cut (in %) on the exposure ( default is 10%)\n";
print "\n";

exit 2;

}