#!/usr/bin/env perl
use vars qw ($queueOB, $queueST);


while (<>) {
#example line of the OB.txt file:
#187   360   28    35    PSPS_PV3_OB_SLICE_22 3800.5985297 ipp101.0
#settings for the queuer
my @split = split (/\s+/, $_);

$minra = $split[0];
$maxra = $split[1];
$mindec = $split[2];
$maxdec = $split[3];
$datastore = $split[4];
$drive = $split[6];

$pspssurvey = 'PV3';
$ippsurvey = '3PI';
$datarelease = '3';
#$datastore_stub = 'PSPS_PV3_TEST_';
$dvodb = 'LAP.PV3.MERGE';
#$dvodb_path = '/data/ipp094.0/eugene/pv3.cam.20150607/catdir.master';

$dvodb_path = '/data/ipp105.0/eugene/3pi.dvo.masters/3pi.pv3.20170216';


$skychunk = $datastore; 

#@stage = ("OB","P2","ST","IN","FO","FW");
@stage = ("FW");
   $queueP2 = 0;
   $queueST = 0;
   $queueOB = 0;
   $queueFW = 0;
   $queueFO = 0;


$date1='2007-05-01';
$date2='2016-09-01';

foreach $mid (@stage) {
    $loadername = $skychunk;
   $main::queueP2 = 0;
   $main::queueST = 0;
   $main::queueOB = 0;
   $main::queueFW = 0;
   $main::queueFO = 0;

    if ($mid =~ /FW/) {
	$main::queueFW = 1;
	$main::queueST = 0;
	$main::queueOB = 0;
	$main::queueP2 = 0;
	$main::queueFO = 0;
    }


 #  print "$mid $main::queueP2  $main::queueST $main::queueOB $main::queueFW $main::queueFO\n";

$date = "2016-04-26";

#md 04 is 150 2.2

$box = 1;

$rabox = $maxra - $minra;
$decbox = $maxdec -$mindec;


#first check if $maxra - $minra is a multiple of box
if ($rabox % $box == 0) {
#    print "ra boxsize seems ok\n";
} else {
    print "ra boxsize not ok\n";
}
if ($decbox % $box == 0) {
#    print "dec boxsize seems ok \n";
} else {
    print "dec boxsize not ok\n";
}

$compra = ($maxra-$minra) % $box;
if ($compra == 0) {
#    print "min/max ra is a multiple of $box\n";
} else {
    print "min/max ra is not a multiple of $box\n";
}

$compdec = ($maxdec - $mindec) % $box;
if ($compdec == 0) {
 #   print "min/max dec is a multiple of $box\n";
} else {
    print "min/max dec is not a multiple of $box\n";
}

    @dates = ("$date1", "2011-01-01","2012-01-01","2013-01-01","2014-01-01", "$date2");

#	  $num = $#dates;
	
#for ($iii=0;$iii<=4; $iii++) {  
    chomp $iii;
    # $date1= $dates[$iii];
    # $date2= $dates[$iii+1];
    
	#$mid = "ALL";
	$file = "$skychunk"."_ITC.dat";
	open FILE, ">", $file or die $!;
	
#     $sk = $skychunk."_"."$iii";
     $sk = $skychunk."_ITC";
	print FILE "$sk\n";  #skychunk name
	print FILE "$datastore\n";                     #datstore product
	print FILE "IPP_PSPS\n";                       #datastore type
	print FILE "1\n";                              #publish to datastore
	print FILE "$dvodb\n";                         #dvolabel
	print FILE "$dvodb_path\n";                    #dvopath
	print FILE "$minra\n";                            #rmin 
	$ra1 = $ra + $rabox;                      #rmax 
	print FILE "$maxra\n";                           
	print FILE "$mindec\n";                           #decmin 
	$dec1 = $dec + $decbox;  
	print FILE "$maxdec\n";                          #decmax
	print FILE "$box\n";                           #box
	print FILE "/data/$drive/ipptopsps/$dvodb\n";              #output dir
	print FILE "3\n";                              #datarelease
	print FILE "1\n";                              #delete local
	print FILE "1\n";                              #delete datastore
	print FILE "1\n";                              #delete dxlayer
	print FILE "$date\n";                          #epoch wtf
	print FILE "$ippsurvey\n";                     #survey
	print FILE "$pspssurvey\n";                    #pspssurvey
	print FILE "$main::queueP2\n";                              # queueP2
	print FILE "$main::queueST\n";                              #queuest
	print FILE "$main::queueOB\n";                              #queueob
        print FILE "0\n";#queuedf
        print FILE "0\n";#queuedo
        print FILE "$main::queueFW\n";#queuefw
        print FILE "$main::queueFO\n";#queuefo
        print FILE "0\n";#queuefg
        print FILE "0\n";#queuego
	print FILE "1\n";                              #actibe
	print FILE "1\n";                              #parallel
        print FILE "use_original\n";                     #use orig for p2
        print FILE "not_reproc\n";                       #ST 
	print FILE "$date1\n";
	print FILE "$date2\n";
	close FILE;


	print "ippjython queue.py edit < $file\n";
	#print "ra: $ra dec: $dec box: $box\n";
	$count++;
    }


}
