#!/usr/bin/perl

use Shell qw(date ls);
use Time::Local;

$abbaArchive="abbaArchive";
$fimmaArchive="fimmaArchive";
$modisArchive="modisArchive";
$viirsArchive="viirsArchive";
$datadir = "/data/ftp/pub/FIRE";
#$datadir = "/data/ftp/pub/FIRE/test";
$homedir = "/data/ftp/pub/FIRE/test/Jade";
#$homedir = "/home/oper/hms";
($sec, $min, $hour, $mday, $month, $year, $wday, $yday, $isdat) = localtime(time - 3*24*60*60);
$year += 1900;

print "\$year is $year and \$mday is $mday \$month is $month \$hour $hour \$min $min\n";

$jday1 = ymd2yjday($year, $month, $mday);
#$julian_day = $year . $julian_day;
print " \$year $year \$jday1 $jday1\n";

($sec4, $min4, $hour4, $mday4, $month4, $year4, $wday, $yday, $isdat) = localtime(time - 2*24*60*60);
$year4 += 1900;
$jday4 = ymd2yjday($year4, $month4, $mday4);
print "\$year4 is $year4 and \$mday4 is $mday4 \$month4 is $month4 \$jday4 $jday4\n";

#added for one day before merge
($sect, $mint, $hourt, $mdayt, $montht, $yeart, $wday, $yday, $isdat) = localtime(time - 4*24*60*60);
$yeart += 1900;
$jdayt = ymd2yjday($yeart, $montht, $mdayt);
print "\$yeart is $yeart and \$mdayt is $mdayt \$montht is $montht \$jdayt $jdayt\n";

#die;

#Get age at midnight
$hr=0;
$min=0;
$midnight=&age_min($year,$jday1,$hr,$min);
$dayend=$midnight+1440;
print "\$midnight $midnight \$dayend $dayend\n";

$abbafinal="wf_abba$year$jday1.v65.txt";
$fimmafinal="fimma_r$year$jday1.dat";
$modisfinal="MOD14.$year$jday1.v5.0.01.$dat";
$viirsfinal="VIIRSNDE_$year$jday1.v1.0.txt";

#GOES - WFABBA section
system("ls $datadir/ABBA/f${year}${jday1}*g* > $homedir/$abbaArchive");
system("ls $datadir/ABBA/f${year4}${jday4}*g* >> $homedir/$abbaArchive");
open(OUTPUTABBA, ">$datadir/ABBA/$abbafinal");
open(ABBA, "<$homedir/$abbaArchive");
 @lines = <ABBA>;
 #print @lines;
 chomp @lines;
print OUTPUTABBA "    Lon,     Lat,   Temp4,  Temp11,  Size(km2),  Temp(K),  Ecosys,Fireflag,  Sat,  YearDay,   Time\n";
 foreach $linein (@lines) {
   chomp $linein ;
#   print "$linein\n";
   @sat=split(/\./, $linein);
   @sat2=split(/g/, $sat[2]);
   $id=@sat2[1];
   @date=split(/f/, $linein);
   #print "@date\n";
   #$fyear=substr($date[1],0,4);
   $fyear=substr($date[2],0,4);
   $fday=substr($date[2],4,3);
   #$fday=substr($date[1],4,3);
   $fhr=substr($date[2],7,2);
   #$fmin=substr($date[1],9,2);
   $fmin=substr($date[2],9,2);
   $fage=&age_min ($fyear,$fday,$fhr,$fmin);
   
   #print "\$fage $fage \$fyear $fyear \$fday $fday \$fhr $fhr \n";
   if ($id eq '11') {$off = 9;}
   elsif ($id eq '12') {$off = 6;}
   else {$off = 8;}
   $off=$off*60;
   if (($fage >= $midnight+$off) and ($fage <= $dayend + $off)) {
     $val=0;
     open(TEMP, "<$linein");
     @newlines = <TEMP>; close(TEMP);
     foreach $newlinein (@newlines) {
       chomp $newlinein;
       if($val != 0){
	   @lonlat=split(/,/,$newlinein);
	   if((@lonlat[0]<0 or @lonlat[0]>160) and @lonlat[1]>7){
	       print OUTPUTABBA "$newlinein,     $id,  $fyear$fday,   $fhr$fmin\n";
	   }# end if
       }# end if
       $val=1;
     }# end foreach
#     system("rm $linein");
   }# end if
 }# end foreach
close(OUTPUTABBA);
close(ABBA);
#Archival
system ("mv $datadir/ABBA/$abbafinal $datadir/ABBA/TXT_ARCHIVE/");
system("ls $datadir/ABBA/f${yeart}${jdayt}*g*");










#=====================Sub ymd2yjday()=========================================
sub ymd2yjday {

my ($year, $month, $mday) = @_;

my (%month2jday) =  (
                           'noleapyear' =>
                             [
                                0,
                                31,
                                59,
                                90,
                                120,
                                151,
                                181,
                                212,
                                243,
                                273,
                                304,
                                334,
                                365,
                             ],

                           'leapyear'   =>
                             [
                                0,
                                31,
                                60,
                                91,
                                121,
                                152,
                                182,
                                213,
                                244,
                                274,
                                305,
                                335,
                                366,
                             ],
                        );

my ($currentyear, $jday);

#print "Inside FUNC \$year $year \$month is $month \$mday $mday\n";
if ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0)) {
  $currentyear = 'leapyear';
}
else {
  $currentyear = 'noleapyear';
}
#print "\$month2jday->{$currentyear}->[$month] is $month2jday{$currentyear}[$month]\n";

#$jday = $month2jday{$currentyear}[$month] + $mday;
$jday = sprintf("%3.3d", $month2jday{$currentyear}[$month] + $mday);
#print "\$jday is $jday\n";
return ($jday);
}

#==========================sub age_min=======================================
sub age_min
{
$year=$_[0];
$julday=$_[1];
$hour=$_[2];
$minute=$_[3];

$fourcentury=$year % 4;
$century=$year % 100;
$leapyear=$year % 4;

#Compute age in minutes
if($fourcentury == 1) {$age= $year*527040 + $julday*1440 + $hour*60 + $minute}
elsif($century == 1) {$age= $year*525600 + $julday*1440 + $hour*60 + $minute}
elsif($leapyear == 1) {$age= $year*527040 + $julday*1440 + $hour*60 + $minute}
else {$age= $year*525600 + $julday*1440 + $hour*60 + $minute}

return($age);
}

