#!/usr1/local/bin/perl 
#
# script to modify FITS files by writing new keywrds to
# the required extension headers
#
# Lorraine Breedon 12/5/97
# HEASARC NASA/GSFC/HSTX
#
#

require "getopts.pl";


#================================================================#
#
# See if there are any flags
#


&Getopts('h');
if (defined $opt_h) {
    print <<EOHELP1;
    NAME
      mod_rmfs -- a script that will modify FITS file extension headers 
                      

    USAGE
      
 

    DESCRIPTION 
      This script allows users to input either an ascii file containing 
      a list of FITS files (filename only; one for each mission/instrument; 
      one file-per-line) OR the FITS files may input manually via prompting.  
      The existence of each file is checked. If any file does not exist a
      warning is given. Duplicity of filenames is also checked. 
      
      New keywords are added to the required FITS file extension headers

      
     PARAMETERS

      input option [character string]
         The user supplied string specifying either "manual" or "list.ascii"
         implying manual input of FITS files or the user has compiled a list of
         FITS filenames in an ascii file named  "list.ascii".
 



    SEE ALSO

       
EOHELP1
exit;
    }


require "utils.pl";

# initialise variables
$counter=0;
$i=0;
$j=0;
$version="1.0.0";


# check the command line arguements
$narg=$#ARGV+1;
if (($narg < 5) || ($narg > 5)) {
    die "** usage : mod_fits_asca_sis.perl filename alias chip thresh nextn **";
} else {
# check file actually exists
   if (-e $ARGV[0]) {
      $filename=$ARGV[0];
      $sis_alias=$ARGV[1];
      $sis_chip=$ARGV[2];
      $sis_thresh=$ARGV[3];
      $nextn=$ARGV[4];
      for ($i=1; $i<=$nextn; $i++) {
          $file_extn=$filename."+".$i;
# get EXTNAME value in each extn header of file
           $fkeyparfile="/home/lhea3/breedon/pfiles/fkeypar.par";
           if (-e $fkeyparfile) {
              unlink $fkeyparfile;
           }

           @result = &runcom("fkeypar $file_extn extname");
           &num_char($fkeyparfile);
           print "$file_extn\n";
           $extname_value=substr($string,12,$nchar-2);
           print "$extname_value\n";
           $ccnm_val=substr($extname_value,0,3);
           $cbd1_method=substr($extname_value,4,1);
           if ($cbd1_method =~ /G/) {
               $cbd1_method2=$cbd1_method."AUS";
           } else {
              $cbd1_method2="DIPLOD";
           }

           $cbd1_val="METHOD(\"$cbd1_method2\")";
           $cbd2_val="SPLIT($sis_thresh)";
           $cbd3_grade=substr($extname_value,5,1);
           $cbd3_val="GRADE(\"$cbd3_grade\")"; 
           $cbd4_corr=substr($extname_value,7,1);
           $cbd4_val="ECHO($cbd4_corr)";
           $detnam="CCD".$sis_chip;
           $instrume="SIS".$sis_alias;
                     

# create ascii file containing the keyword to be added to the 1st
# extension header
	   $file1="INPUT1.ascii";
           if (-e $file1) {
               unlink $file1;
           }
           open (OUT, "+>$file1") || die "cannot open $file1"; 
           print OUT "TELESCOP = ASCA / Telescope (mission) name\n";
           print OUT "INSTRUME = $instrume / Instrument name\n"; 
           print OUT "DETNAM = $detnam / Detector name\n";
           print OUT "CCLS0001 = BCF / dataset is basic calibration file \n";
           print OUT "CDTP0001 = DATA / Calibration file contains data \n";
           print OUT "CCNM0001 = $ccnm_val / Type of Calibration data \n";
           print OUT "CBD10001 = $cbd1_val / Calibration boundary: model fitting method\n";
           print OUT "CBD20001 = $cbd2_val / Calibration boundary: split threshold value \n"; 
           print OUT "CBD30001 = $cbd3_val / Calibration boundary: grade value \n";
           print OUT "CBD40001 = $cbd4_val / Calibration boundary: echo value \n";
 
           print OUT "CVSD0001 = 18/03/93 / UTC date when calibration should first be used\n";
           print OUT "CVST0001 = 11:06:20 / UTC time when calibration should first be used\n";
          print OUT "CDES0001 = 'SIS energy data contains modelled response features' / \n";
      

           close(OUT);

# modify each extension header
           @result = &runcom("fmodhead $file_extn $file1");
#           @result = &runcom("fparkey 'SIS energy data contains modelled #response features /' $file_extn CDES0001 add+");
       }
   } else {
      print " ** ARGV[0] does not exist ** \n";
      die " ** program MOD_FITS_ASCA_SIS.PERL $version terminated **";
   }
}
           
 
          



# ... subroutine to calculate the number of characters for the keyword value in 
# the fkeypar.par file
sub num_char{
                  
    open (FIN, "$fkeyparfile") || die "cannot open $fkeyparfile";
    while(<FIN>) {
         $line=substr($_,0,5);
         if ($line =~ /value/) {
            $string=$_;
            chop($string);
            $fstbit=index($string,"h");
            $sndbit=index($string,"K");
            $start=$fstbit+4;
            $end=$sndbit-5;
            $nchar=$end-$start+1;
         }
                       
     }
     close(FIN);
}
# ...end of subroutine 
                        
print " \n";
print " \n";
print " ** MOD_FITS_ASCA_SIS.PERL $version finished **\n";






    

