#!/usr/bin/perl


# This script computes MIPS CSMM parameters for a photometry observation,
# given constraints on the number and separation of dither positions.
#
# C. Engelbracht 2003-03-09


# set constants

$ogain = 32.635;	# optical gain (arcsec in CSMM space / arcsec on sky)
$dgain = 2.17;		# fine DAC gain (arcsec in CSMM space / LSB)
$pscale24 = 2.55;	# 24um pixel scale (arcsec / pixel)
$pscale70 = 9.84;	# 70um pixel scale (arcsec / pixel)
$pscale70f = 4.99;	# 70um fine pixel scale (arcsec / pixel)
$pscaleSED = 10.13;	# SED pixel scale (arcsec / pixel)
$pscale160 = 15.99;	# 160um pixel scale (arcsec / pixel)


# get command-line options

if (defined ($ARGV[0])) {$array = $ARGV[0];}
else {
   print "Please enter array type (24/70/70f/SED/160):  ";
   chop ($array = <STDIN>);
}
$_ = "24 70 70f SED 160";
if (!m/$array/) {
   print "Array type must be 24, 70, 70f, SED, or 160, not \"$array\".\n";
   exit;
}

if (defined ($ARGV[1])) {$stimcycle = $ARGV[1];}
else {
   print "Please enter stimcycle (add 1 for a stimflash, if needed):  ";
   chop ($stimcycle = <STDIN>);
}

if (defined ($ARGV[2])) {$sep1 = $ARGV[2];}
else {
   print "Please enter separation, in pixels, between DCEs 1 and 0:  ";
   chop ($sep1 = <STDIN>);
}

if (defined ($ARGV[3])) {$sep2 = $ARGV[3];}
else {
   print "Please enter separation, in pixels, between DCEs 2 and 0:  ";
   chop ($sep2 = <STDIN>);
}


# set pixel scale and adjust gain, if necessary

CASE: {

   if ($array eq "24") {$pscale = $pscale24; last CASE;}

   if ($array eq "70") {$pscale = $pscale70; last CASE;}

   if ($array eq "70f") {$pscale = $pscale70f; $dgain = 2.52; last CASE;}

   if ($array eq "SED") {$pscale = $pscaleSED; $dgain = 2.62; last CASE;}

   if ($array eq "160") {$pscale = $pscale160; last CASE;}

   die "Could not determine plate scale";

}


# compute gain factor to covert pixels to fine DAC units (negative sign
# converts to CSMM parity)

$gain = -$pscale * $ogain / $dgain;


# compute stepoffset

$stepoffset = sprintf "%0.0f", $sep2 * $gain / 2;


# compute relpos2 - relpos1

$delta = $sep1 * $gain - $stepoffset;


# assume relpos1 is 2048 and compute list of positions

$relpos1 = 2048;
$relpos2 = sprintf "%0.0f", $relpos1 + $delta;

for ($i = 0; $i < $stimcycle; $i++) {

   if ($i % 2) {$list[$i] = $relpos2 + ($i+1) * $stepoffset;}
   else {$list[$i] = $relpos1 + ($i+1) * $stepoffset;}

}


# find extreme positions

$min = $list[0];
$max = $list[0];
for ($i = 0; $i <= $#list; $i++) {
   if ($min < $list[$i]) {$min = $list[$i];}
   if ($max > $list[$i]) {$max = $list[$i];}
}


# subtract average of extreme positions from relpos1 and relpos2

$diff = sprintf "%0.0f", ($max + $min) / 2 - 2048;
$relpos1 -= $diff;
$relpos2 -= $diff;


# print out results

print "\nINPUT PARAMETERS\n";
print "Array = $array\n";
print "stimcycle = $stimcycle\n";
print "Separation between DCEs 1 and 0 = $sep1 pixels\n";
print "Separation between DCEs 2 and 0 = $sep2 pixels\n";

print "\nOUTPUT PARAMETERS\n";
print "stepoffset = $stepoffset\n";
print "relpos1 = $relpos1\n";
print "relpos2 = $relpos2\n";
