# / / / / / / / / / / / / /
#   bias_4k.pl
# / / / / / / / / / / / / /
#
#
# This program bias subtracts a file using the overscan region
# 

$file_name = "$ARGV[0]";

@timeData = localtime(time);
#print join(' ', @timeData);

#print "@timeData \n";

#0  1  2  3  4 5   6 7  8
#23 58 10 19 1 113 2 49 0
$seconds = $timeData[0];
$minutes = $timeData[1];
$hour = $timeData[2];
$day = $timeData[3];
$month_num = $timeData[4];
$year_num = $timeData[5];
$dayofweek_num = $timeData[6];
$standard_time_num = $timeData[8];

$year = $year_num + 1900;

if ($month_num == 0) { $month = "Jan" }
if ($month_num == 1) { $month = "Feb" }
if ($month_num == 2) { $month = "Mar" }
if ($month_num == 3) { $month = "Apr" }
if ($month_num == 4) { $month = "May" }
if ($month_num == 5) { $month = "Jun" }
if ($month_num == 6) { $month = "Jul" }
if ($month_num == 7) { $month = "Aug" }
if ($month_num == 8) { $month = "Sep" }
if ($month_num == 9) { $month = "Oct" }
if ($month_num == 10) { $month = "Nov" }
if ($month_num == 11) { $month = "Dec" }

if ($dayofweek_num == 0) { $dayofweek = "Sun" }
if ($dayofweek_num == 1) { $dayofweek = "Mon" }
if ($dayofweek_num == 2) { $dayofweek = "Tue" }
if ($dayofweek_num == 3) { $dayofweek = "Wed" }
if ($dayofweek_num == 4) { $dayofweek = "Thu" }
if ($dayofweek_num == 5) { $dayofweek = "Fri" }
if ($dayofweek_num == 6) { $dayofweek = "Sat" }

if ($standard_time_num == 0) { $standard_time = "EST" }
if ($standard_time_num == 1) { $standard_time = "EDT" }

# Tue Feb 19 10:58:58 EST 2013

$date_num = "$dayofweek $month $day $hour".":"."$minutes".":"."$seconds $standard_time $year";

print "$date_num \n";

$iraf_script="iraf_script.csh";
open(IRAF,">>$iraf_script") || die("This file will not open!");

print IRAF "cl << END  >log.file \n";
print IRAF "set clobber = yes \n";
print IRAF "set imtype=\"fits\" \n";	
print IRAF "noao \n";
print IRAF "imred \n";
print IRAF "bias \n";
print IRAF "images \n";
print IRAF "imutil \n";

print IRAF "colbias $file_name.fits $file_name.1.fits bias=[1:32,1:2032] trim=[33:2064,1:2032] median- interac- function=\"legendre\" order=4  \n";
print IRAF "colbias $file_name.fits $file_name.2.fits bias=[1:32,2033:4064] trim=[33:2064,2033:4064] median- interac- function=\"legendre\" order=4  \n";
print IRAF "colbias $file_name.fits $file_name.3.fits bias=[4097:4128,1:2032] trim=[2065:4096,1:2032] median- interac- function=\"legendre\" order=4  \n";
print IRAF "colbias $file_name.fits $file_name.4.fits bias=[4097:4128,2033:4064] trim=[2065:4096,2033:4064] median- interac- function=\"legendre\" order=4  \n";

print IRAF "imarith $file_name.fits[33:4096,1:4064] * 1.0 $file_name.trim.fits ver-  \n";

print IRAF "imcopy $file_name.1.fits[1:2032,1:2032] $file_name.trim.fits[1:2032,1:2032] ver- \n";
print IRAF "imcopy $file_name.2.fits[1:2032,1:2032] $file_name.trim.fits[1:2032,2033:4064] ver-  \n";
print IRAF "imcopy $file_name.3.fits[1:2032,1:2032] $file_name.trim.fits[2033:4064,1:2032] ver-  \n";
print IRAF "imcopy $file_name.4.fits[1:2032,1:2032] $file_name.trim.fits[2033:4064,2033:4064] ver-  \n";

print IRAF "imdel $file_name.1.fits \n";
print IRAF "imdel $file_name.2.fits \n";
print IRAF "imdel $file_name.3.fits \n";
print IRAF "imdel $file_name.4.fits \n";

print IRAF "rename $file_name.trim.fits $file_name.b.fits \n";

print IRAF "hedit $file_name.b.fits BIAS4K \"$date_num\" add+ del- ver- show- upd+ \n";
print IRAF "hedit $file_name.b.fits OVERSCAN \"[1:32,*] and [4097:4128,*]\" add+ del- ver- show- upd+ \n";
print IRAF "hedit $file_name.b.fits CCDSEC \"[33:4096,1:4064]\" add+ del- ver- show- upd+ \n";

print IRAF "logout \n";	
print IRAF "END \n";

close(IRAF);

system("sh $iraf_script");

system("rm $iraf_script");
system("rm uparm*");
