#! /usr/local/bin/perl

# Script to generate map-files on the web: to be read by OHIO

$infile = "sites.processed";
$rinexdir = "/net/josh9/matthijs/csrc/yolo/1999/rinex";
@days = ('189', '190', '193', '194', '195', '196');
$coordfile = "coords.all";
$mapfile1 = "/home/archive/web-docs/images/maps/general/proc.yolo.all.map";
$mapfile2= "/home/archive/web-docs/images/maps/general/proc.yolo.base.map";
$mapfile3= "/home/archive/web-docs/images/maps/general/proc.yolo.local.map";
open(IN, "${infile}"); 
open(OUT, ">${mapfile1}"); 
open(OUTNET1, ">${mapfile1}"); 
open(OUTNET2, ">${mapfile2}"); 
open(OUTNET3, ">${mapfile3}"); 
foreach $day (@days){
	$key = "DAY$day";
	$mapday = "/home/archive/web-docs/images/maps/general/proc.yolo.day${day}.map";
	open($key , ">$mapday");
}

@networks = ('base', 'local');

while(<IN>){
	foreach $b (@networks){
	 if( $_ =~ /${b}/) {
	 	push(@{$b}, substr($_, 0, 4));
	 }
	}
}

print("Sites not in $coordfile: \n");

foreach $a (@base){
	$coord = `grep -i $a $coordfile | cut -c5-45`;
	$coord =~ s/^\s+//;
	@coords = split(/ +/, $coord);
	$lat = $coords[0];
	$lon = $coords[1];
	print(OUT join("\t", 'PT', $lon, $lat, $a, '-999', '-999', 'diamond/sdi_lb.gif', 'DEFAULT', "\n"));
	print(OUTNET2 join("\t", 'PT', $lon, $lat, $a, '-999', '-999', 'diamond/sdi_lb.gif', 'DEFAULT', "\n"));
	foreach $day (@days){
		$tmp = `ls $rinexdir/${a}${day}*`;
		if($tmp ne "") {
			$key = "DAY$day";
			print($key join("\t", 'PT', $lon, $lat, $a, '-999', '-999', 'diamond/sdi_lb.gif', 'DEFAULT', "\n"));
		}
	}
}

foreach $a (@local){
	$coord = `grep -i $a $coordfile | cut -c5-45`;
	$coord =~ s/^\s+//;
	@coords = split(/ +/, $coord);
	$lat = $coords[0];
	$lon = $coords[1];
	print(OUT join("\t", 'PT', $lon, $lat, $a, '-999', '-999', 'diamond/sdi_gr.gif', 'DEFAULT', "\n"));
	print(OUTNET3 join("\t", 'PT', $lon, $lat, $a, '-999', '-999', 'diamond/sdi_gr.gif', 'DEFAULT', "\n"));
	foreach $day (@days){
		$tmp = `ls $rinexdir/${a}${day}*`;
		if($tmp ne "") {
			$key = "DAY$day";
			print($key join("\t", 'PT', $lon, $lat, $a, '-999', '-999', 'diamond/sdi_gr.gif', 'DEFAULT', "\n"));
		}
	}
}


close(OUT);
