#!/usr/local/bin/perl

($fld,$RA,$Dec) =@ARGV;

$info =`bin/radec2xy BUL_SC$fld $RA $Dec`;
#$info =`bin_sun/radec2xy BUL_SC$fld $RA $Dec`; for Sun machine
($x,$y) = split(" ", $info);
print "x= $x y= $y\n";

$pi = 3.14159265358979;
#####################################################################3
$Nbin = 16; #   $binsize  = 128;
#--------------Read VImaps----------------------------------------------
%EVI = %EVIe =%AV= %AVe= %AI=%AIe=();
$nall=0;
open("DATA", "zcat EVImaps/sc$fld.map.gz |");
$nfread=0;
while($line=<DATA>){
  ($nf,$xi,$yi,$EVI,$EVIe, $AV,$AVe, $AI,$AIe,$Nall,$Nallcut,$VIsdevall,$NRC,$NRCcut,$VIsdevRC,$gi) =split(" ", $line,);
  $nall++;
  ($w,$xia,$yia,$w,$VIm,$VIme,$w,$Im,$Ime,$w,$$Vm,$Vme)=split(" ",$line)if $line=~/ALL/;
  ($w,$binsize,$w,$nfgood,$npoor,$w,$nall,$sumnall,$w,$ncut,$sumncut,$w, $VIallsdev)
                                                =split(" ", $line)if $line =~ /binsize/;
  ($w,$EVI0e,$w,$AV0e,$w,$AI0e)=split(" ",$line)if $line=~/AV0err/;

  next if $line =~ /#/;
  $EVI{$nf}  = $EVI;
  $EVIe{$nf} = $EVIe;
  $AV{$nf}  = $AV;
  $AVe{$nf} = $AVe;
  $AI{$nf}  = $AI;
  $AIe{$nf} = $AIe;
  $gi{$nf} = $gi;
  $nfread++;
}
$Nbin=$xia;
$binsize  = 2048/$Nbin;
$ximax = $Nbin -1;
$yimax = $Nbin *4 -1;
$nall = $nall-3;
#------------------------------------------------------------------

$xi =  int($x/ $binsize);
$yi =  int($y/ $binsize);
$nf = $xi+$yi*($ximax+1);
#$EVI0e = sprintf("%.5f", $EVI0e);
#$AV0e = sprintf("%.5f", $AV0e);
#$AI0e = sprintf("%.5f", $AI0e);
if ($EVI{$nf}==0){print "NO map\n"; exit;}
printf ("%2d %2d   %.4f %.4f  %.4f %.4f  %.4f %.4f   %.4f  %.4f  %.4f  %d\n",
$xi,$yi,$EVI{$nf},$EVIe{$nf},$AV{$nf},$AVe{$nf},$AI{$nf},$AIe{$nf},$EVI0e,$AV0e,$AI0e,$gi{$nf});

##########################################################
sub numerically { $a <=> $b; }
sub descending { $b <=> $a; }

