#!/usr/bin/perl
#
#
#


# preberi meritve
open IME, $ARGV[0];
$i=0;
while (<IME>){
	if(/^#/) {next}   # preskoci komenterje
	($name[$i],$timeobs[$i],$mag[$i],$merr[$i],$sharp[$i],$chi[$i],$pier[$i],$frame[$i])=split;
	$timeobs[$i] -= 0.5;  # popravek iz MJD v  JD
	$i++;
}


# naredi seznam zvezd
$k=0;

for($j=0; $j<$#name; $j++) {
	$it = $name[$j];
	$found = 0;
	for ($m=0; $m<$k; $m++) {
	     if ($it eq $imena[$m]) {
		 	$found = 1;
		 	last;
		}
	}
	if ($found==0) {
		$imena[$k] = $name[$j];
		#print $imena[$k],"\n";
		$k++;
	}
}
print "stevilo zvezd je ",$#imena,"\n";


# stevilo zvezd je k
# povprecje, standardna dev.
# za vse zvezde
open_html();
for ($kk=0; $kk<$k;$kk++) {

	# for zanka tece po vseh zvezdah in se racuna

	$ms=0;                      # vsota magnitud
	$n=0;                       # stevilo meritev za k-to zvezdo
	$mstd=0;                    # standardna deviacija
	$wms=0;                     # vsota magnitud z utezjo
	$wmstd=0;                   # standardna dev z utezjo
	$w=0;	                    # vsota utezi
	$smerr=0;                    # vsota napak posameznih meritve
	for ($i=0; $i<$#name; $i++) {
		if($name[$i] eq $imena[$kk]) {

			$ms    += $mag[$i];
			$wi     =1/(0.05+$merr[$i]);     # i-ta utez je obrat napake
			$wms   += $mag[$i]*$wi;		
			$w     +=$wi;
			$smerr += $merr[$i]; 

			$t[$n]=$timeobs[$i];       #prepisi cas in mag v nek array
			$m[$n]=$mag[$i];
			$e[$n]=$merr[$i];
			$n++;
		}
	}
	$mp=$ms/$n;                       #povprecna magnituda za k-to zvezdo
	$mmerr=$smerr/$n;                 #povprecna napaka posamesne meritve 
	$wmp= $wms/$w;                    #uravnotezeno povprecje

	for ($i=0; $i<$#name; $i++) {
		if($name[$i] eq $imena[$kk]) {
			$mstd = $mstd + ($mp - $mag[$i])*($mp - $mag[$i]);
			$wi   =1/(0.05+$merr[$i]);
			$wmstd= $wmstd + $mstd*$wi;
		 }
	}
	if ($n > 50) {	
		#omejitev za stevilo meritev
		$std= sqrt ( $mstd/ ($n-1));
		$wstd= sqrt($wmstd/($w*($n-1)));
		$vi = $std/$mmerr;

		if ($vi>2.0 and $std>0.1)  {
			# kriterij za spremenljivko
			printf "%20s %8.3f %8.3f %8.3f %8.3f %8.0f\n",$imena[$kk],$mp,$std,$mmerr,$vi,$n;

			open DAT, ">$imena[$kk].dat";
			for ($l=0; $l<$n; $l++) {
				print DAT $t[$l],"\t",$m[$l],"\t",$e[$l], "\n";  #primerne podatke shrani v
         	            		                                      #datoteke
	 		}
			close DAT;
    			$vi = int(100*$vi+0.5)/100;
    			$std= int(100*$std+0.5)/100;
			graf();
			write_html();
		}
	}
}
close_html();

sub graf
{
open(GP,">$imena[$kk].gp") || die "$myname: cannot open file outfile: $!\n";

print GP "set terminal gif small xffffff x000000 x404040 x0000ff xffa500 x66cdaa xcdb5cd\n";
#print GP "set terminal gif small \n";
print GP "set output \"$imena[$kk].gif\"\n";
print GP "set title \"$imena[$kk] \" \n";
#print GP "set ylabel \"$filter mag\"\n";
print GP "set format x \"%4.2f\"\n";
print GP "set format y \"%4.1f\"\n";
print GP "f(x) = $mp \n";
print GP "set yrange [$mp - 1:$mp + 1.2] reverse\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 xtics 0.04\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 label 'std=$std vi=$vi'  at graph 0.6,1.05\n";
print GP "set xlabel \"JD 2454216+ \"\n";
print GP "plot f(x) title 'mean' with lines 2,\"$imena[$kk].dat\" using 1:2:3  notitle  with errorbar 1\n";

print GP "set output \n";
close GP;
# narisi
`gnuplot -bg white $imena[$kk].gp`;
return 1;
}




sub open_html
{
open HT,">variable.html";
printf HT "<HTML>\n";
printf HT "</head>\n";
printf HT "<h1>Spremenljive zvezde v kopici M53</h1>\n";
printf HT "<h3>opazovanje 2007-04-25 (JD 2454216) </h3>\n";
printf HT "<BODY BGCOLOR=\"#ffffff\">\n";

printf HT "<a HREF=\"./imlist.lst\">Seznam slik in zero magnitude</a><br>\n ";
printf HT "<a HREF=\"./comparison.txt\">Izbrane primerjalne zvezde</a><br>\n ";
printf HT "<a HREF=\"./starlist.lst\">Seznam vseh izmerjenih zvezd</a><br>\n ";
printf HT "<a HREF=\"./novariable.html\">Tabela nespremenljivih zvezd</a><br>\n ";
printf HT "<a HREF=\"./znaneRR.dat\">Znane spremenljivke</a><br>\n ";

printf HT "<p>izbor zvezd, ki ustreza pogoju (za spremenljivke):\n";
printf HT "<ul><li> standardna deviacija serije std > 0.2\n";
printf HT "<li> razmereje vi=std/merr > 2.0\n";
printf HT "</ul>\n";

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=\"$imena[$kk].gif\"><BR>\n";
printf HT "                             </P>\n";
printf HT "                     </TD>\n";
$col++;
}



