#!/usr/bin/perl 

############################################################################
#
# Name: bbep2.pl

# Purpose: retrieve bbep2 output from satepsanon, then distribute it to DDS
# 
# Developers: Y. Zhu (ACENTIA) June 22, 2015
#             CCR02696
#              
#
##############################################################################

$startTime=`date`;
print "pull/push bbep2 data start at $startTime\n";


$ENV{INPUT_HOST}='satepsanon.espc.nesdis.noaa.gov';
$ENV{BBEP2_DIR}='/data/ftp/pub/FIRE/BBEP-geo/';
$ENV{INDATA}="/gpfs1/ops/test/shared/GBBEP/bbep2";
#$ENV{INDATA}="/gpfs1/ops/prod/shared/BBEP2/data";
$ENV{WORK}=join('',$ENV{HOME},"/BBEP2/scripts");
$ENV{DIST}='/gpfs1/ops/common/bin';
$ENV{WEB_DATA}='/home/Pbbep/BBEP2/data/web';
$ENV{WEB_DIR}='/data/web/mirror/www.ospo.noaa.gov/htdocs/data/land/bbep2';

chdir("$ENV{WORK}");

&time;

#-- clean old data

print "clean old data from INDATA\n";
unlink (glob("$ENV{INDATA}/burned*")) or warn "can't delete files: $!";
unlink (glob("$ENV{INDATA}/biomass*")) or warn "can't delete files: $!";

#--test from satepsanon

    $modisDate=$preyear;
    if(($preyday>=1)&&($preyday<10))
    {
        $modisDate.="00";
        $modisDate.=$preyday;
    }
    if(($preyday>=10)&&($preyday<100))
    {
        $modisDate.="0";
        $modisDate.=$preyday;
    }
    if(($preyday>=100)&&($preyday<400))
    {
        $modisDate.=$preyday;
    }

    print "modisDate: $modisDate \n";


#--retrieval BBEP2 output from satepsanon

print "pull bbep2 data from satepsanon\n";
    open(SFTP,'|sftp oper@satepsanon.nesdis.noaa.gov');
    print SFTP "cd $ENV{BBEP2_DIR}\n";
    print SFTP "lcd $ENV{INDATA}\n";
#    print SFTP "mget *$filemmdd*.gz\n";
    print SFTP "mget *$filemmdd*23*.jpg\n";
    print SFTP "mget *$filemmdd*23.txt\n";
#    print SFTP "cd $ENV{BBEP2_DIR}/PREVIOUS_DAYS\n";
#    print SFTP "mget *$filemmdd*.gz\n";
    print SFTP "quit\n";
    close(SFTP);


#--distribute output to DDS

print "start distribute BBEP2 2 burned data to DDS\n";

#opendir (DIR, $ENV{INDATA}) or die "can't open $ENV{INDATA}:  $!";
##my @files= readdir DIR;
#my @files= grep {gz} readdir DIR;
#closedir DIR;

#my $fileline;
#my $file;
#my $ddsRetCode;

#foreach $fileline ( @files ) {
#  print "fileline: $fileline\n";
#  chomp($fileline);
#  $file=$fileline;
##  print "file: $file\n";

#  if($file=~m/^burned_area_([0-9]{8})_([0-9]{2})_([0-9]{2})_1km.grib2.gz$/ || $file=~m/^burned_area_([0-9]{8})_([0-9]{2})_([0-9]{2})_12km.grib2.gz$/)
#          {
#            print "file sent: $file\n";
#            $ddsRetCode = system("$ENV{DIST}/dist_file $ENV{INDATA}/$file");
#                if ( $ddsRetCode ) {
#                   print "error in distribution of $file\n";
#                   exit 99;
#                   }
#           }
#  } #-end of foreach  

 print "end of distribution data to DDS\n";


#--Prepare images and data files foe web server(gp5/gp50)
print "clean old web data\n";
system("rm $ENV{WEB_DATA}/*");

print "copy data from sfs to web_data\n";
system("cp $ENV{INDATA}/*$filemmdd*NA.jpg $ENV{WEB_DATA}/burned_area_NA.jpg");
system("cp $ENV{INDATA}/*$filemmdd*QA.jpg $ENV{WEB_DATA}/burned_area_QA.jpg");
system("cp $ENV{INDATA}/*$filemmdd*txt $ENV{WEB_DATA}/biomass_burning.txt");

#system("cp $ENV{INDATA}/*$filemmdd*NA.jpg.gz $ENV{WEB_DATA}/burned_area_NA.jpg.gz");
#system("cp $ENV{INDATA}/*$filemmdd*QA.jpg.gz $ENV{WEB_DATA}/burned_area_QA.jpg.gz");
#system("cp $ENV{INDATA}/*$filemmdd*txt.gz $ENV{WEB_DATA}/biomass_burning.txt.gz");

#print "unzip data files\n";
#system("gunzip $ENV{WEB_DATA}/burned_area_NA.jpg.gz");
#system("gunzip $ENV{WEB_DATA}/burned_area_QA.jpg.gz");
#system("gunzip $ENV{WEB_DATA}/biomass_burning.txt.gz");

#--Send images and data files to web server (gp5/gp50)

print "Send BBEP2 images and data files to web server\n";
    open(SFTP,'|sftp ospodata@gp50');
    print SFTP "cd $ENV{WEB_DIR}\n";
    print SFTP "lcd $ENV{WEB_DATA}\n";
    print SFTP "mput *.jpg\n";
    print SFTP "mput *.txt\n";
    print SFTP "quit\n";
    close(SFTP);
print "End of sending data to web server\n";


$endTime=`date`;
print "pull/push bbep2 data end at $endTime\n";

print "end\n";


##############################################################################
# get time of current and previous day, month and year
#
#        Developers: Xiaoyang Zhang
#                   Essic/UMD at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
################################################################################

sub time {
    $now_string = localtime;
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime(time);

    $curyear=$year+1900;
    $curyday=$yday+1;
    $curmon=$mon+1;
    $preyday=$yday;
    $preyear=$curyear;
    $premday=$mday-1;
    $premon=$curmon;

print "current:: year==$curyear; month==$curmon;  DOY==$curyday;  Day==$mday\n";
#print "utchour==$hour, min==$min\n";

    $preyday=$yday;
    $preyear=$curyear;

    if(($curmon==1)&&($mday==1))
    {
        $preyear=$curyear-1;
        $preyday=365;
        $premon=12;
        $premday=31;

        if(($preyear%4==0)&&($preyear%100!=0))   ##leap year
        {
            $preyday=366;
        }
    }
    
#print "Pre:: preyear==$preyear; premon==$premon;  preDOY==$preyday;  preDay==$premday\n";

    if($premday<1)
    {
        if(($curmon==5)||($curmon==7)||($curmon==10)||($curmon==12)) 
        {$premday=30;}

        if(($curmon==2)||($curmon==4)||($curmon==6)||($curmon==8)||($curmon==9)||($curmon==11)) 
        {$premday=31;}
        if($curmon==3){
            $premday=28;
            if(($preyear%4==0)&&($preyear%100!=0))
            {
                $premday=29;
            }
        }
        if($curmon==1){
            $premon=12;
        }else{
            $premon=$curmon-1;
        }
    }

## print "preMOMN==$premon, pre_mday==$premday;pre_yearday==$preyday; preYear=== $preyear\n";
print "Pre:: preyear==$preyear; premon==$premon;  preDOY==$preyday;  preDay==$premday\n";

###YYYYMMDD

    if($premon<10) {$premon=join('',"0",$premon);}
    if($premday<10) {$premday=join('',"0",$premday);}
    $filemmdd=join('',$preyear,$premon,$premday);
 print "filemmdd==$filemmdd\n";

##fYYYYDDDHHMM.
##$filename="f";
    $filename=$preyear;
    if(($preyday>=1)&&($preyday<10))
    {
        $filename.="00";
        $filename.=$preyday;
    }
    if(($preyday>=10)&&($preyday<100))
    {
        $filename.="0";
        $filename.=$preyday;
    }
    if(($preyday>=100)&&($preyday<400))
    {
        $filename.=$preyday;
    }

    $yydd=$filename;
    $filename="f";
    $filename.=$yydd;
 print "filt-filename==$filename\n";
}


