#!/usr/bin/perl
#     
#     B.Dintinjana
#     september 2016
#
#    ./zvezde3.pl obslist.std 
#


# 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<$#imena;$kk++) {

	# for zanka tece po vseh zvezdah in se racuna za vsako posebej
	#print "zvezda ",$imena[$kk]," ",$kk,"\n";


	$n=0;                       # stevilo meritev za k-to zvezdo
	$wms=0;                     # vsota magnitud z utezjo
	$w=0;	                    # vsota utezi

	undef @t;
	undef @m;
	undef @e;
	
	for ($i=0; $i<$#name; $i++) {
		if($name[$i] eq $imena[$kk]) {

			$wms   += $mag[$i] / ($merr[$i]*$merr[$i]);		
			$w     += 1.0 / ($merr[$i]*$merr[$i]);		

			$t[$n]=$timeobs[$i];       #prepisi cas in mag v nek array
			$m[$n]=$mag[$i];
			$e[$n]=$merr[$i];
			$n++;
		}
	}
	$wmp=$wms/$w;                  #povprecna utezena magnituda za k-to zvezdo
	# vse meritve za zvezdo k so sedaj zbrane v vektorjih t,m,e
	
	$p = 0.0;
	for ($i=0; $i<$#t; $i=$i+2) {

		$dm1 = ($m[$i] - $wmp) / $e[$i];
		$dm2 = ($m[$i+1] - $wmp) / $e[$i+1];

		$p += $dm1 * $dm2;
		#print $i
		
	}
	# k je stevilo parov meritev ali B,V ali dveh zaporednih
	$k = int($#t/2);

	if ($k > 25) {	
		#omejitev za stevilo meritev
		#printf "%20s %8.3f %8.3f %8.0f\n",$imena[$kk],$wmp,$I,$#m;
		$I = $p * sqrt(1.0/($k*($k-1.0)));
		
		if ($I>10.0)  {
			# kriterij za spremenljivko
			printf "%20s %8.3f %8.3f %8.0f\n",$imena[$kk],$wmp,$I,$#m;

			open DAT, ">$imena[$kk].dat";
			for ($l=0; $l<$n; $l++) {
				print DAT $t[$l]-int($t[$l]),"\t",$m[$l],"\t",$e[$l], "\n";  #primerne podatke shrani v
         	            		                                      #datoteke
	 		}
			close DAT;
    			$I = int(100*$I+0.5)/100;
    			$wmp= int(100*$wmp+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 png small size 287,215 \n";
print GP "set output \"$imena[$kk].png\"\n";

print GP "set title \"$imena[$kk] \" \n";
print GP "set format x \"%4.2f\"\n";
print GP "set format y \"%4.1f\"\n";
print GP "f(x) = $wmp \n";
print GP "set yrange [$wmp - 0.7:$wmp + 0.9] 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 mxtics 2\n";
print GP "set xtics 0.02\n";
print GP "set nogrid \n";
print GP "set pointsize 1\n";
print GP "set style line 1 lc rgb 'blue' lt 1 lw 2 pt 7 ps 0.6\n";
print GP "set style line 2 lt 2 lc rgb 'red'  lw 2\n";
print GP "set label 'V=$wmp vI=$I'  at graph 0.6,1.05\n";
$mjd=int($timeobs[$kk]);
print GP "set xlabel \"JD 24$mjd+ \"\n";
print GP "plot f(x) title 'mean' with lines,\"$imena[$kk].dat\" using 1:2:3  notitle  with yerrorbars ls 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 M15</h1>\n";
printf HT "<h3>opazovanje 2018-12-11 (JD 2458464) </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=\"./variable.html\">Tabela spremenljivih 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> variability index > 10.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].png\"><BR>\n";
printf HT "                             </P>\n";
printf HT "                     </TD>\n";
$col++;
}



