#!/usr/local/bin/perl


%FORM = &init_html;


$filter=$FORM{FILTER};
$seeing=$FORM{SEEING};
$exp=$FORM{EXPTIME};
$SNR=$FORM{SNR};
$mag=$FORM{MAG};
$moon=$FORM{MOON};
$AB=$FORM{AB};
$apsize=$FORM{APSIZE};

$ABlabel{1}="AB mags";
$ABlabel{0}="Johnson mags";

$JtoS{u}=-1.03; # conversion between SDSS mags and Johnson
$JtoS{vs}=0.04;
$JtoS{g}=0.007;
$JtoS{r}=-.164;
$JtoS{i}=-0.44;
$JtoS{z}=-0.63;


$ZP{u}=22.9+$JtoS{u}*(1-$AB); # e/s is this magnitude 
$ZP{vs}=22.4+$JtoS{vs}*(1-$AB); #scaled from CFHT
$ZP{g}=24.3+$JtoS{g}*(1-$AB);   #which are right for
$ZP{r}=23.8+$JtoS{r}*(1-$AB);   #ABmags
$ZP{i}=23.6+$JtoS{i}*(1-$AB);
$ZP{z}=22.9+$JtoS{z}*(1-$AB); 


#Johnson mags here for sky
$SKY{u}{DARK}=22.8-$JtoS{u}*$AB; #mag/sq arc sec 
$SKY{vs}{DARK}=22.5-$JtoS{vs}*$AB;
$SKY{g}{DARK}=21.5-$JtoS{g}*$AB;
$SKY{r}{DARK}=20.7-$JtoS{r}*$AB;
$SKY{i}{DARK}=19.3-$JtoS{i}*$AB;
$SKY{z}{DARK}=18.9-$JtoS{z}*$AB;

$SKY{u}{GREY}=20.0-$JtoS{u}*$AB; #mag/sq arc sec 
$SKY{vs}{GREY}=21.3-$JtoS{vs}*$AB;
$SKY{g}{GREY}=20.8-$JtoS{g}*$AB;
$SKY{r}{GREY}=20.4-$JtoS{r}*$AB;
$SKY{i}{GREY}=19.2-$JtoS{i}*$AB;
$SKY{z}{GREY}=18.7-$JtoS{z}*$AB;

$SKY{u}{BRIGHT}=17.0-$JtoS{u}*$AB; #mag/sq arc sec 
$SKY{vs}{BRIGHT}=18.5-$JtoS{vs}*$AB;
$SKY{g}{BRIGHT}=18.5-$JtoS{g}*$AB;
$SKY{r}{BRIGHT}=18.9-$JtoS{r}*$AB;
$SKY{i}{BRIGHT}=18.2-$JtoS{i}*$AB;
$SKY{z}{BRIGHT}=18.0-$JtoS{z}*$AB;



$flatnoise=0.003;
$RN=5.0; #e/pixel
$pixsize=0.5; #in arcsec/pixel;

if ($apsize == 0) {
    $aperture = 1/$pixsize * $seeing*0.75; #optimal aperture in pixels
}
else {$aperture =$apsize/$pixsize;}
if ($aperture < 2) {$aperture=2;} #undersampled!
$pix=$aperture*$aperture*3.141592653589793;

#
# fig out apcor as a function or aperture size/FWHM
# a very approximate relation
$apcor=exp(-1.5*(($aperture*$pixsize/$seeing)-0.75))*.62; 
($apcor)=sprintf("%4.3f",$apcor);

($apsizearcsec)=sprintf("%3.2f",$aperture*$pixsize);

print <<START;

<html>
<head>
<title>SkyMapper telescope SNR Calculator Output</title>
</head>
<BODY BGCOLOR="#ffffff" text="#000000"><P><P>
<center><IMG SRC="http://msowww.anu.edu.au/~brian/mssso_banner.jpg">
<h2>SkyMapper telescope SNR Calculator Output</h2></center>
<hr><hr>

ZP $ZP{$filter}<br>
SKY $SKY{$filter}{$moon}<br>


START

if ($seeing < 1.0) {
    print "<hr><br>Warning, These data will be undesampled.<hr><br>\n";
}

if ($mag < 3) {
    $mag=0;
}
print "<h3><center>Input parameters:</bf></center><br>\n";
print "Seeing: $seeing arcseconds<br>Phase of moon: $moon Time<br>Object magnitude: $mag in the $filter filter ($ABlabel{$AB})<hr><br>\n";
print "Apsize: $apsizearcsec arcseconds<br>Aperture Correction: $apcor (correction for r=infinity to r=aperture in mags)<br>";
if ($SNR eq "") {
    print "Exposure Time: $exp seconds</h3>\n";
}
else {
    print "SNR desired: $SNR</h3>\n";
}

print <<TABLE;
<center>
<table border=2 cellspacing=0 cellpadding=5 ><tr>
<th>integration time <br>(seconds)</th>
<th>total sky<br>(electrons)</th>
<th>total object<br>(electrons)</th>
<th>total noise<br>(electrons)</th>
<th>S/N ratio</th>
</tr>

TABLE


    if ($SNR eq "") {
	
	for ($t=$exp/2;$t<$exp*2.1;$t*=2) {
	    
	    $skycounts= 10**(($ZP{$filter}-$SKY{$filter}{$moon})/2.5)*$t*$pixsize*$pixsize;  #count/pixel
	    
	    $objcounts=10**(($mag-$ZP{$filter}+$apcor)/-2.5)*$t;  #optimum 0.75FWHM is down by 0.62 mags from infinite aperture
	    	    
	    $noise=sqrt(($flatnoise*$skycounts)*($flatnoise*$skycounts)*$pix+$skycounts*$pix+$RN*$RN*$pix);  #RN+skynoise+flatnoise in aperture
	    
	    $SN=$objcounts/$noise;
	    if ($t==$exp) {
		printf("<tr><td>%7.0f</td><td>%9.2e</td><td>%9.2e</td><td>%9.2e</td><td>%7.1f</td></tr><br>\n",$t,$skycounts,$objcounts,$noise,$SN);
	    }
	    else {
		printf("<tr><td background=#999999>%7.0f</td><td>%9.2e</td><td>%9.2e</td><td>%9.2e</td><td>%7.1f</td></tr><br>\n",$t,$skycounts,$objcounts,$noise,$SN);
	    }
	}
    }
else {
    $done=0;
    for ($t=1;;$t*=1.01) {
	$skycounts= 10**(($ZP{$filter}-$SKY{$filter}{$moon})/2.5)*$t*$pixsize*$pixsize;  #count/pixel
	$objcounts=10**(($mag-$ZP{$filter}+$apcor)/-2.5)*$t;  #optimum 0.75FWHM is down by 0.62 mags from infinite aperture
	$noise=sqrt(($flatnoise*$skycounts)*($flatnoise*$skycounts)*$pix+$skycounts*$pix+$RN*$RN*$pix);  #RN+skynoise+flatnoise in aperture
	$SN=$objcounts/$noise;
	if ($t>36000) {
	    printf("<tr><td background=#999999>%7.0f</td><td>%9.2e</td><td>%9.2e</td><td>%9.2e</td><td>%7.1f</td></tr><br>\n",$t,$skycounts,$objcounts,$noise,$SN);
	    printf("<tr><td background=#999999>Cannot achieve in less than 10 hours</td></tr>\n");
	    last;
	}
	if ($SN > $SNR) {
	    printf("<tr><td background=#999999>%7.0f</td><td>%9.2e</td><td>%9.2e</td><td>%9.2e</td><td>%7.1f</td></tr><br>\n",$t,$skycounts,$objcounts,$noise,$SN);
	    last;
	}
    }
}   


print "</table></center>\n";
print <<END;
<br>
<a href=http://msowww.anu.edu.au/~brian/SkyMapper_SNR.html>Return to SNR Calculator</a>
<br><P>Brian Schmidt brian\@mssso.anu.edu.au<BR>
last update 26May2006.<br> 
END





	
	
sub init_html {
# Print out a content-type for HTTP/1.0 compatibility
    print "Content-type: text/html\n\n";
    print "<html>\n\n";
    if ( $ENV{'CONTENT_LENGTH'} > 0 ) {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Get the input
    } else {
        $buffer = $ENV{'QUERY_STRING'};
    }
    @pairs = split(/&/, $buffer); # Split the name-value pairs
    foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /; # Un-Webify plus signs and %-encoding
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ s/([;<>\*\`&\$!?#\(\)\[\]\{\}'"\\])/\\$1/g; # Get rid of dangerous characters
        $FORM{$name} = $value;
    }
    %FORM;
}
    
    
 


