#!/usr/bin/perl
#
# program za analizo spremenljivih zvezd
# pozeni tako:
#   starsplot.pl
#
# maj.2003 B.Dintinjana & skupina studentov
#          Lenart Krecic, Mateja Virant,
#          Anja Znidarsic, Barbara Valentinèiè, Mateja Lavriè
#
open (DAT,"reference.dat");

#
# magnituda referencne zvezde (sestej za vec zvezd)
#
$mag1=11.0;



open_html();
$i=0;
while (<DAT>){
     if(/^#/){
        next
     }
    ($var_name[$i],$shit) = split;
    printf "$var_name[$i]\n";

    # na hitro razbijemo zbirno datoteko kar v lupini
    `grep " $var_name[$i] " magnitude.dat > $var_name[$i].mag`;

    graf();
    write_html();
    $i++;

}
close_html();




sub open_html
{
open HT,">variable.html";
printf HT "<HTML>\n";
printf HT "</head>\n";
printf HT "<h1>Variable star experimet</h1>\n";
printf HT "<h3>M3 observation on 2003-04-01</h3>\n";
printf HT "<BODY BGCOLOR=\"#ffffff\">\n";
printf HT "<a HREF=\"./reference.dat\">Reference star list</a> ";
printf HT "<TABLE BORDER=0>\n";
printf HT "	<TBODY>\n";
printf HT "		<TR>\n";
$col=0;
}



sub close_html
{
printf HT "		</TR>\n";
printf HT "   </TBODY>\n";
printf HT "</TABLE>\n";
printf HT "<P><BR><BR>\n";
printf HT "</P>\n";
printf HT "</BODY>\n";
printf HT "</HTML>\n";
close(HT);
}

sub write_html
{
if($col eq 3){
	printf HT "		</TR>\n";
	printf HT "		<TR> \n";
	$col=0;
}
printf HT "			<TD >\n";
printf HT "				<P>\n";
printf HT "				<IMG SRC=\"$var_name[$i].gif\"><BR>\n";
printf HT "				</P>\n";
printf HT "			</TD>\n";
$col++;
}





sub graf
{
open GP, "|gnuplot  2>/dev/null 1>&2";
#open GP, ">test.gp";
print GP "set terminal png color small xffffff x000000 x404040 x0000ff xffa500 x66cdaa xcdb5cd\n";
print GP "set output \"$var_name[$i].gif\"\n";
print GP "set title \"$var_name[$i]\"\n";

print GP "set format x \"%4.2f\"\n";
print GP "set format y \"%5.2f\"\n";

print GP "f(x) = c \n";
print GP "fit  f(x) '$var_name[$i].mag' using 2:(\$3+$mag1) via c \n";

print GP "set yrange [c - 0.5:c + 0.5] reverse\n";
print GP "set xrange [0.33:0.53]\n";

print GP "set ticslevel 4\n";
print GP "set mytics 5\n";
print GP "set ytics -5,0.5,20\n";
print GP "set nogrid  \n";
print GP "set pointsize 1\n";
print GP "set linestyle 1 lt 2 lw 2 pt 3 ps 0.5 \n";
print GP "set size 0.45,0.45\n";

print GP "set xlabel \"JD 2452731+ \"\n";
print GP "plot f(x) title 'mean' with lines 2,'$var_name[$i].mag' using (\$2-2452731):(\$3+$mag1):4  notitle with errorbar 1\n";

print GP "set output \n";
close GP;

}
