#!/usr/bin/perl


use Math::Utils qw(:utility); 
#use Geo::Coordinates::DecimalDegrees;
                        
#Script to convert Asci table .master to html

$file = $ARGV[0];
$title =  $ARGV[1];
$Nfield= 22;

$dss_search = 2;


if (-e $ARGV[$0]) {
	open (INFILE,"<$file");
	print ("Openning file $file\n");
	} else { 
	print ("file $file not found\n");
	}
open(OUTFILE,"> $file.html") || die "Could not create $file.html\n";

#Print header

print OUTFILE
 " <HTML> 
<head><title> $title  </title>

<script type=\"text/javascript\"  src=\"sorttable.js\"></script>

<style type=\"text/css\">
/* Sortable tables */
table.sortable a.sortheader {
    background-color:#eee;
    color:#666666;
    font-weight: bold;
    text-decoration: none;
    display: block;
}
table.sortable span.sortarrow {
    color: black;
    text-decoration: none;
}
</style>

</head>


<body bgcolor=\"white\"> 
<h1> <center> $title </center></h1> 
<br> 
View  <a href=\"$file\"> ascii table</a>. Download <a href =\"RUN_target_to_html.pl\"> script</a>.
<br> 
Click on the ID to view images <br> 
<br> 
Click on the column header to sort
<hr> <br> 

<center>
<table id=\"t1\" class=\"sortable\" cellpading=3 border=4>
 
 

<font size=\"-2\">


\n";

$comment = 0;


$I=0;
while (<INFILE>) {
	   	
		
		
		 @columns = split(/,/);
	     $ctr=0;
		 $RA   = $columns[10];
		 $DEC  = $columns[11];
		
		 @radms = split(/:/,$RA);
		 $RA_d=$radms[0];
		 $RA_m=$radms[1];
		 $RA_s=$radms[2];
		
		 $ra2000 = 15.0 * ($RA_d + ($RA_m / 60.0) + ($RA_s / 3600.0));
		 #$ra2000 = 15.0 * dms2decimal($RA_d,$RA_m,$RA_s);
		 
		 @Decdms = split(/:/,$DEC);
		 $Dec_d=$Decdms[0];
		 $Dec_m=$Decdms[1];
		 $Dec_s=$Decdms[2];
		 
		  if ( $DEC =~/^[+]/ ) {
             $dec2000= (abs($Dec_d) + (abs($Dec_m) / 60.0) + (abs($Dec_s) / 3600.0));
         } else{
            $dec2000=-1* (abs($Dec_d) + (abs($Dec_m) / 60.0) + (abs($Dec_s) / 3600.0));
        }   
    
         #$dec2000 = dms2decimal($Dec_d,$Dec_m,$Dec_s);

         #print("a[0]:",$a," ",$dec2000);

		 $NameID  = $columns[2];
		 $id_off = $columns[3];
		 $Texp   = $columns[4];
		 $PSF    = $columns[5];
		 $version= $columns[7];
		 $maxAuto= $columns[12];
	     $ebv_planck    = $columns[14];
		
		
        # $obj = new Astro::Coords(ra=>'15:09:45.4534434', dec=>'03:00:45',type=>"J2000", format=>'sexagesimal');
        # ($ra2000,$dec2000) = ($obj -> ra(format=>'deg'), $obj->dec());
        
        
	    #my ($ra2000, $dec2000) = palFk45z($RA, $DEC, 2000.0);
		print STDOUT "\n$RA,$DEC,\n";
		print STDOUT "\n$DEC,$dec2000,\n";
		
		 print OUTFILE " <tr>\n";
					
		 if ($NameID =~ "---"){
		 	my $comment = 1;
			my $ctr = $ctr +1;
			print STDOUT $comment;
		}
		 
		if ($I == 0){
	      	        	print OUTFILE "<th align=right> ID \n";
	      	        			} else {
	      	        	print OUTFILE "<td align=right> $I \n";
	      	 }
		
		while($ctr<$Nfield){
		
	      	      
	      	my $Field=$columns[$ctr];
	       	        	
	      	        	
			if ($I == 0){
	      	        	print OUTFILE "<th align=right> $Field\n";
	      	        } else {
	      	        				
				if ($ctr == 1 && ($comment == 0 && $NameID !~ /[#]/)){
	      	       	       		 
	      	        	 print OUTFILE "<td align=right> <a href=\"$Field.html\" target='blank' > $Field</a> \n";
#	    print OUTFILE " <td> <a href=\"http://archive.stsci.edu/xcorr.php?RA=$RA&DEC=$DEC&max_records=10&action=Search&missions[]=WFPC1&missions[]=WFPC2&missions[]=WFPC2_ASN&missions[]=FOC&missions[]=ACS&missions[]=STIS&missions[]=NICMOS\"> HST Search </a> \n"; 
						#print OUTFILE " <td> <a href=\"http://www.eso.org/observing/bin/skycalcw?cmd=o&site=v&sdate=2006+01+01&edate=2006+12+31&ra=$RA_d%3A$RA_m%3A$RA_s&dec=%2B$Dec_d%3A$Dec_m%3A$Dec_s\"> ESO vis </a> \n";
						#print OUTFILE "  <a href=\"http://galex.stsci.edu/GSC2/GSC2DataReturn.aspx?RAH=$RA_d&RAM=$RA_m&RAS=$RA_s&DSN=+&DD=$Dec_d&DM=$Dec_m&DS=$Dec_s&EQ=2000&SIZE=60&SRCH=Radius&FORMAT=HTML&CAT=GSC23&HSTID=&GSC1ID=\"> GSC-2  </a> \n";
						#print OUTFILE " <td> <a href=\"http://www.stsci.edu/hst/HST_overview/TwoGyroMode/AllSkyInformation?RA=$RA_d%3A$RA_m%3A$RA_s&Dec=$Dec_d%3A$Dec_m%3A$Dec_s&Find=Find\"> 2Gyro Vis</a>\n";
						#print OUTFILE " <td> <a href=\"../OH_lines/$Field.skyline.ps\"> OHlines </a>\n";
						if ($ctr==2) {
						    print OUTFILE " <td>  <a href=\"http://skyserver.sdss.org/dr16/en/tools/explore/Summary.aspx?ra=$RA&dec=$DEC\" target='_blank'> $Field</a> \n";
			            }
						$NameID=$Field ;
						#if (!(-e $NameID)) {
		 				#	#print STDOUT "mkdir $NameID\n";
						#	#$command = "mkdir $NameID";
						#	#system($command);
						#	}	
							
						
						open(OUTFILE_object,"> $NameID.html") || die "Could not create  $NameID.html\n";
						
							
						print OUTFILE_object
			"<HTML> 
			<head><title> $NameID  </title>

			 


			<body bgcolor=\"white\"> 
			<h1> <center> $NameID $RA  $DEC </center></h1> 
			<br> 
			 Click on the links to view images <br> 
			<br> 
			 
			<center>
			<table   cellpading=3 border=4>
			<tr>
			 
			<td> Archive </td>
            <td> DECAL </td>
			<td> POSS2 $dss_search'</td>
			<td> POSS IR $dss_search'</td>
			<td> SDSS DR16</td>
			</tr>
			 <tr>
			 
		
			<td> <a href=\"http://archive.stsci.edu/xcorr.php?RA=$RA&DEC=$DEC&radius=1&max_records=10&action=Search&missions[]=GALEX\"> Galex Search </a>  
			<br> <a href=\"http://archive.stsci.edu/xcorr.php?RA=$RA&DEC=$DEC&radius=0.02&max_records=10&action=Search&missions[]=IUE\"> IUE Search </a>  
			<br> <a href=\"http://archive.stsci.edu/xcorr.php?RA=$RA&DEC=$DEC&radius=0.02&max_records=10&action=Search&missions[]=FUSE\"> FUSE Search </a>  
 			<br>
			<a href=\"http://archive.stsci.edu/xcorr.php?RA=$RA&DEC=$DEC&radius=0.02&max_records=10&action=Search&missions[]=WFPC1&missions[]=WFPC2&missions[]=FOC&missions[]=ACS&missions[]=WFC3&missions[]=STIS&missions[]=FUSE\"> HST Search </a>
			</td>
			 
			<td> <a target=\"_blank\" href=\"https://www.legacysurvey.org/viewer/ra=$ra2000&dec=$dec2000&zoom=13&ls-dr9\">
			    <IMG SRC=\"https://www.legacysurvey.org/viewer/jpeg-cutout?ra=$ra2000&dec=$dec2000&height=200&width=200&pixscale=0.3&bands=grz\"> </a></td>


			<td> <a href=\"http://archive.stsci.edu/cgi-bin/dss_search?v=poss2ukstu_red&r=$RA_d%3A$RA_m%3A$RA_s&d=$Dec_d%3A$Dec_m%3A$Dec_s&e=J2000&h=$dss_search&w=$dss_search&f=fits&c=n&fov=NONE&v3=\" >
			<IMG SRC=\"http://archive.stsci.edu/cgi-bin/dss_search?v=poss2ukstu_red&r=$RA_d%3A$RA_m%3A$RA_s&d=$Dec_d%3A$Dec_m%3A$Dec_s&e=J2000&h=$dss_search&w=$dss_search&f=gif&c=n&fov=NONE&v3=\" > </a></td>
			<td> <a href=\"http://archive.stsci.edu/cgi-bin/dss_search?v=poss2ukstu_ir&r=$RA_d%3A$RA_m%3A$RA_s&d=$Dec_d%3A$Dec_m%3A$Dec_s&e=J2000&h=$dss_search&w=$dss_search&f=fits&c=n&fov=NONE&v3=\">
			<IMG SRC=\"http://archive.stsci.edu/cgi-bin/dss_search?v=poss2ukstu_ir&r=$RA_d%3A$RA_m%3A$RA_s&d=$Dec_d%3A$Dec_m%3A$Dec_s&e=J2000&h=$dss_search&w=$dss_search&f=gif&c=n&fov=NONE&v3=\">  </a></td>
			<td> <a href=\"http://skyserver.sdss.org/dr16/en/tools/explore/Summary.aspx?ra=$RA&dec=$DEC&scale=0.396127&width=400&height=400&opt=GST&query=SR(10,20)\"> <IMG SRC=\"http://skyservice.pha.jhu.edu/DR14/ImgCutout/getjpeg.aspx?ra=$RA&dec=$DEC&scale=0.8&width=200&height=200&opt=GST&query=SR(10,20)\"
			</a></td>
			
			</tr>  
			
			 <tr>
			 
			<td> <a href=\"../OH_lines/$NameID.wfpc.fits\" >	       .fits </a></td>

            <td> <a href=\"https://www.legacysurvey.org/viewer/fits-cutout?ra=$ra2000&dec=$dec2000&height=200&width=200&pixscale=0.3&bands=grz\"> DECal grz.fits </a> </td>
            
			<td> <a href=\"http://archive.stsci.edu/cgi-bin/dss_search?v=poss2ukstu_red&r=$RA_d%3A$RA_m%3A$RA_s&d=$Dec_d%3A$Dec_m%3A$Dec_s&e=J2000&h=$dss_search&w=$dss_search&f=fits&c=n&fov=NONE&v3=\" > 
			    PossR.fits </a> </td>
			<td><a href=\"http://archive.stsci.edu/cgi-bin/dss_search?v=poss2ukstu_ir&r=$RA_d%3A$RA_m%3A$RA_s&d=$Dec_d%3A$Dec_m%3A$Dec_s&e=J2000&h=$dss_search&w=$dss_search&f=fits&c=n&fov=NONE&v3=\">
			    UKIRT.fits </a> 
			    <a href=\"http://irsa.ipac.caltech.edu/cgi-bin/2MASS/IM/nph-im_pos?type=at&ds=asky&POS=$RA_d%3A$RA_m%3A$RA_s+%09+$Dec_d%3A$Dec_m%3A$Dec_s&subsz=30&date=&scan=&coadd=&key=&band=A\"> 2MASS  </a>
			<td><a href=\"http://skyserver.sdss.org/dr16/SkyserverWS/ImgCutout/getjpeg?ra=$RA&dec=$DEC&scale=0.396127&width=400&height=400&opt=GST&query=SR(10,20)\"> SDSS (jpeg) </a></td>
			 (fits)
			 </tr> 
			 
			 </table> 
		 
			<br> <br>
			
			</table   cellpading=3 border=4>
			<tr>
			<td> HST image
			<td> Reduced Cube
			</tr>
			
			</table>
			 
			 

			\n";
 			#$command = "awk '{print \$0}' Run_Summary2.hdr >   $NameID.Obs";
			#system ($command);
			#$command = "more GTO*List | grep $NameID >>  $NameID.Obs";
			#system($command  ) ;				
			#$command = "HST_Run_to_html.pl	  $NameID.Obs";
			#system($command);
			 	
							
							
							
					 
	      	        	        }else{
					
						 	print OUTFILE "<td align=right> $Field \n";
						 
	      	        	        }
				}
	      	       
	      	        $ctr=$ctr+1	
		}#while
		
		 
		 
		 
		
		
	print OUTFILE " </tr>\n";
		
	 
	
		
		
	
	 
	 
	 
	 
	 
	 
	$I=$I+1;
	 
} #while

print OUTFILE "
</font>
</table></center><hr>
<br> <hr>
<address> N. Bouche <a href=\"mailto: nicolas.bouche\@univ-lyon1.fr\"> nicolas.bouche\@univ-lyon1.fr </a></address>
 
 
<!-- hhmts start -->
  <script language=\"JavaScript\">
 <!-- 
 var LastUpdated = document.lastModified;
 document.writeln (\"Page last updated on \" + LastUpdated);
 // -->
 </script>
 
<!-- hhmts end -->
</body>
</html> ";



