#!/usr/bin/perl 

###############################################################################
# Burned area and biomass burning emission calculation from MODIS-AVHRR-GOES 
#
#      INPUT:
#          Active fire data from GOES, AVHRR, and MODIS 
#          Fuel loadings from MODIS vegetation properties
#          Fuel moisture from AVHRR VCI
#          Combustion and emission factors for various species
#
#
#      OUTPUT:
#	biomass_burning_yyyymmdd_hh_hh.nc-- 
#             contains burned area, emissions, fire location, ecosystem type, fire pixel size, 
#             and fuel loading in NetCDF4.
#	burned_area_yyyymmdd_hh_hh_12km.grib2--
#             cumulated burned area within 6 hours with a grid of 12km.
#	burned_area_yyyymmdd_hh_hh_1km.grib2--
#             cumulated burned area within 12 hours with a grid of 1km.
#	burned_area_yyyymmdd_hh_hh_NA.jpg--
#             spatial pattern of burned area
#	burned_area_yyyymmdd_hh_hh_QA.jpg--
#             statistics of burned area for quality check.
#	biomass_burning_yyyymmdd_hh_hh.txt--
#             contains burned area, emissions, fire location, ecosystem type, fire pixel size, 
#             and fuel loading in a ASCII format.
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 January 2012
#
#
#      Modified on Oct. 28, 2015  Y.Zhu (Maximus)
#      CCR02696 migrate from gp75 to geo system
#
#
###############################################################################


&checks; 

#do "/home/oper/BBEP2/paths.pl" or                 
#    die "Could not run paths.pl"; 

do "/home/Pbbep/BBEP2/scripts/paths.pl" or
    die "Could not run paths.pl";           

chdir("$ENV{WORK}");

&mklog; 
&time;  ##get time
&Files; ## collect fire data
&ALLrearangfireLONLAN; ##formalize fire data and cluster fire observations
&ALLfire_fit_DUR_LONLAN; ## generate diurnal pattern of burned area
&lanlonBA;    ## reformat burned area for generating output
&QA_BurnedArea;  ## staristic analysis of burned ara 
&vcidata;  ## collect AVHRR VCI data
 
###&vcidata_local;
&emission;   ## calculate biomass burning emissions
&hourlyascii;  #generate ascii data of emissions for other users         
&creatnetcdf4; #generate netCDF file for pm2.5 and burned area
&idljpgmap; #generate jpg using idl
&grid;  ##generate burned area in grid cell
&create_grib2; ##generate a GRIB2 format for burned area
&mvoutput; ##re-locate output files
&endlog;  #ends log file 

&file_check_email;  ##check processing error 

        print "end\n";





###############################################################################
# Obtain fire data from GOES and HMS and other ancillary data 
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 July 2011
###############################################################################



sub Files{
    $Lomin=-180;  ##change with th GOES LALONdata
    $Lomax=-45;
    $Lamin=0;
    $Lamax=80;

    &getGOES;
    $GOESdaily=$outfileGO;
   
    &getHMS;
   
    $StandardCurve="$ENV{STINPUT}/fittedfiresizeLST.txt";
    $GOESGrid_E="$ENV{STINPUT}/lon_lat_east"; 
    $GOESGrid_W="$ENV{STINPUT}/lon_lat_west"; 
    $MatchedareaHms="burn_";
    $MatchedareaHms.=$HMSdaily;
    $GoeHms="temp1";  ###GoesModisAvhrr_daylalon_GOES_daylalon1";
    $GoesHmsCluster="temp2"; ### GoesModisAvhrr_daylalon_GOES_cluster_50kmFN_GOES";    
    $GoesHmsFited=$filename0;  ##current day--fitted burned area
    $outputemissionall=$filename0;  ##current day --output emission

    if($hour==0){
	$GoesHmsFited.="00";
	$outputemissionall.="00";
    }
    if(($hour>0)&&($hour<10)){
	$GoesHmsFited.="0";
	$GoesHmsFited.=$hour;
	$outputemissionall.="0";
	$outputemissionall.=$hour;
    }
    if($hour>=10){
	$GoesHmsFited.=$hour;     ##current hour
	$outputemissionall.=$hour;
    }
    $GoesHmsFited.="fitted_burnedarea";   ###fires during 24 hour before current time
    $outputemissionall.=".out_ascii";  ## all emissions

$Temp_BAFlag="$ENV{STINPUT}/Temp_BAFlag.bin"; #added by Pack_BA
$Temp_BACum="$ENV{STINPUT}/Temp_BACum.bin"; ##added by Pack_BA
$grid_burn1km_raw="temp_burn1km"; #added by Pack_BA

    &time_check('get files successfully');    #Prints time and status to log file  
}


###############################################################################
# Extract daily hms fires from GOES "hms**prelim.txt" 
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 July 2011
###############################################################################


sub Daily_hms_extract_fire_time{
    my   $day=0;
    my   $mon=0;
    my   $year=0;

###NA region
    $x1=-180;  ##change with th GOES LALONdata
    $x2=-45;
    $y1=0;
    $y2=80;
  

    $hmsfilename2d=(split('\.',$hmsfilename0))[0];
    if($prehour==0){
	$hmsfilename2d.="00";
    }
    if(($prehour>0)&&($prehour<10)){
	$hmsfilename2d.="0";
	$hmsfilename2d.=$prehour;
    }
    if($prehour>=10){
	$hmsfilename2d.=$prehour;
    }

    $hmsfilename2d.=".prelim.txt";  ##file name for combined HMS fire data in current and previous days
    open(OUT, ">$hmsfilename2d");
    print OUT " RepYear, RepDOY, RepHour, Minute, Lon, Lat, Satellite, Ecosystem, Year, DOY, Hour\n";

## fires in previous day
    @totalfire=NULL;
    open(HALFF,"<$hmsfilename") or $newerr=1;
    @totalfire=<HALFF>;
    close HALFF;

    &time_check("could not open file of $hmsfilename fle") if $newerr;
    $newerr=0;

    $j=@totalfire;  ##number of element of the array  total size from 0--($j-1)
 
##fires in current day
    @hms_fire=NULL;
    open(HALFF,"<$hmsfilename0") or $newerr=1;   
    @hms_fire=<HALFF>;
    close HALFF;
    &time_check("couldn't open file of $hmsfilename0. It's possible sometime") if $newerr;
    $newerr=0;

    $i=0;
    foreach $line(@hms_fire){
	chomp($line);
	if($i>0){
	    $totalfire[$j]=$line;
	    $j=$j+1;	
	}
	$i++;
    }
 
    
    @modis=NULL;
    $i=0;
    $modis[$i]=" RepYear, RepDOY, RepHour, Minute, Lon, Lat, Satellite, Ecosystem, Year, DOY, Hour\n";  
    $i++;
    $num=0;
    foreach $line(@totalfire)
    {
	chomp($line);
	####check longitude, latitude,
	if($num>0)
	{
	    @lindata=split(", ", $line);
	    $lindata[0]=$lindata[0]*10/10;
	    $lindata[1]=$lindata[1]*10/10;
	    $lindata[2]=$lindata[2]*1000/1000;
	    if($lindata[2]=~m!([0-9]{4})([0-9]{3})!){
		$year=$1;
		$day=$2;
	    }
	    $hh=0;
	    $mm=0;
	    $lindata[3]=$lindata[3]*1000/1000;
	    if($lindata[3]=~m!^([0-9]{2})([0-9]{2})$!){
		$hh=$1;
		$mm=$2;
	    }
	    if($lindata[3]=~m!^([0-9]{1})([0-9]{2})$!){
		$hh=$1;
		$mm=$2;
	    }
	    if($lindata[3]=~m!^([0-9]{2})$!){
		$mm=$1;
	    }	    
	    if($lindata[3]=~m!^([0-9]{1})$!){
		$mm=$1;
	    }
	    $hh=$hh*100/100;
	    $mm=$mm*100/100;
	    $data=join('',"f",$year,$day);
	    if(($lindata[4]=~/MODIS/)||($lindata[4]=~/AVHRR/))  ##MODIS+AVHHRR
	    {
		if(($lindata[0]>$x1)&&($lindata[0]<$x2)&&($lindata[1]>$y1)&&($lindata[1]<$y2))
		{
		    if(($data eq $filename)&&($hh>=$prehour))
		    {
			$h2=$hh-$prehour;   ##representative time
			$modis[$i]= join(', ', $preyear, $preyday, $h2, $mm, $lindata[0], $lindata[1], $lindata[4], $lindata[6], $year, $day, $hh);
			print OUT "$modis[$i]\n";
			$i++;
		    }

		    if(($data eq $filename0)&&($hh<$hour)){
			$h2=24-$prehour+$hh; ###representative time
			$modis[$i]= join(', ', $preyear, $preyday, $h2, $mm, $lindata[0], $lindata[1], $lindata[4], $lindata[6], $year, $day, $hh);
			print OUT "$modis[$i]\n";
			$i++;
		    }
		}

	    }  ### 24 hour dada in a new format	    
	}
	$num=$num+1;
    }  #line
    close(OUT);
}


###############################################################################
# Fuse GOES fires and hms fires based on GEOS (East and West) fire footprint 
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 July 2011
###############################################################################

sub ALLrearangfireLONLAN{
    $Lomin=-180;  ##change with th GOES LALONdata
    $Lomax=-45;
    $Lamin=0;
    $Lamax=80;
 
    $infile1=$GOESdaily;
    $outfile=$GoesHmsCluster; ####temp2   

    @all=NULL;
    @files=NULL;
    @data=NULL;
    @dafire=NULL;
    @totalem2=NULL;

    open (TILEID, "<$infile1") or $newerr=1; 
    @files=<TILEID>;
    close TILEID;

    &time_check("could not open file: $infile1") if $newerr;
    $newerr=0;

    $temp="TEMPfile";
    open (OUTtmp, ">$ENV{WORK}/$temp");

    $i=0;
    $k=0;
#GOES

    foreach $tile(@files){
	chomp($tile);
	@data=split(', ',$tile);
	$lonlan=$data[1];  #day of the year
	$lonlan.=$data[4];  ##lon
	$lonlan.=$data[5];  ##lan

	$tile=join(', ',@data);

#    $lonlan.=$data[1];  #day of the year
	$newtile=$lonlan;
	$newtile.=", ";
	$newtile.=$tile;
 
	if($k>0){ 
	    $dafire[$i]=$newtile;
	    $i++;
	}
	$k++;
    }

    @all= sort {(split ', ', $a)[0] cmp (split ', ', $b)[0]}@dafire;
    $k=$i;



#MODIS
#remove repeated spots
    $i=0;
    @x=NULL;
    foreach $tile (@modis){
	chomp($tile);
	@data=split(', ',$tile);
	$data[5]=$data[5]*100/100;
	$xx=join('', $data[1],$data[2],$data[3],$data[4],$data[5]);
	$x1=join(', ',@data);
	$x[$i]=join(';',$xx,$x1);  
	$i++;
    }

    @y = sort {(split ';', $a)[0] cmp (split ';', $b)[0]}@x;
    $xx=0;
    $i=0;
    @modis=NULL;
    foreach $tile (@y){
	chomp($tile);
	@data=split(';',$tile);
	if($xx eq $data[0])
	{
	    $rr=0; 
	}else{
	    $modis[$i]=$data[1];
	    $xx=$data[0];
	    $i++;
	}
    }
 
    @data=NULL;
    @dafire=NULL;
    $i=0;
    foreach $tile (@modis){
	chomp($tile);
	@data=split(', ',$tile);
	if($data[1]>99)
	{
	    $lonlan=$data[1]; 
	}else{
	    if($data[1]<10){
		$lonlan="00";
	    }
	    if(($data[1]>=10)&&($data[1]<100)){
		$lonlan="0";
	    }
	    $lonlan.=$data[1];  #DOY
	}

	$data[4]=$data[4]+0.005;
	$lonlan.=sprintf("%.2f",$data[4]);
	$data[5]=$data[5]+0.005;
	$lonlan.=sprintf("%.2f",$data[5]);
	$data[4]=sprintf("%.2f",$data[4]);
	$data[5]=sprintf("%.2f",$data[5]);

	$T4=0;
	$T11=0;
	$satang=0;
	$pixel=1;
	$frp=0;

	$Size=0.159;

	$Temp=0;
	$Eco=$data[7];
	$Flag=999;  ##MODIS_AVHRR
	$dafire[$i]=join(', ',$lonlan, $data[0], $data[1],$data[2],$data[3],$data[4],$data[5], $satang,$pixel, $T4, $T11, $Size, $Temp, $frp, $Eco, $Flag);

	$i++;
    }

    @totalem2 = sort {(split ', ', $a)[0] cmp (split ', ', $b)[0]}@dafire;

###add GOES fires and hms fires
    foreach $tile (@totalem2){
	chomp($tile);

	$all[$k]=$tile;
	$k++;
    }
    @all2 = sort {(split ', ', $a)[0] cmp (split ', ', $b)[0]}@all;

    print OUTtmp "year day hour min longitude latitude satzen pixelsize fireSize Temp Eco Flag\n";

    $totalline=0;

    foreach $tile (@all2){
	chomp($tile);
	@line=split(', ',$tile); 
	
	if(($line[5]>$Lomin)&&($line[5]<$Lomax)&&($line[6]>$Lamin)&&($line[6]<$Lamax))
	{
###reduced columns from GOES data such as the real year, day, and hour
	    $tt1=join(' ', $line[1], $line[2],$line[3], $line[4],$line[5],$line[6], $line[7],$line[8], $line[11],$line[12],$line[14],$line[15]);
	    print OUTtmp "$tt1\n";
  $totalline= $totalline+1;
	}
    }
    close(OUTtmp);

    if(-e "$ENV{SOURCE}/GOES_EW_lola_Cluster.exe") {$t=0;}
    else{  &time_check('GOES_EW_lola_Cluster.exe does not exist'); }
    if(-e "$GOESGrid_E") {$t=0;}
    else{  &time_check("$GOESGrid_E"); }
    if(-e "$GOESGrid_W") {$t=0;}
    else{  &time_check("$GOESGrid_W"); }
    if(-e "$temp") {$t=0;}
    else{  &time_check("fire data are not integrated"); } 

    system("$ENV{SOURCE}/GOES_EW_lola_Cluster.exe $GOESGrid_E $GOESGrid_W $temp $totalline $outfile");
}



###############################################################################
# Re-arrange fire data for generating diurnal pattern
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 July 2011
###############################################################################


sub AllClusterLONLAN{
 
    $ModAV=0.128;  
    $ModAVF=0.115;  
    $ModAVNF=0.159;  
    $k=0;
 
    foreach $line(@fileso){
	chomp($line);
	@modis=split(', ',$line);

	$m2=scalar(@modis);

	for($ii=0;$ii<$m2;$ii++){
	    $modis[$ii]=$modis[$ii]*10/10;	    
	}

####2012 Feb
	if(($modis[3]>8)&&($modis[3]<=23)) {$modis[3]=15;}
	if(($modis[3]>23)&&($modis[3]<=38)) {$modis[3]=30;}
	if(($modis[3]>38)&&($modis[3]<=53)) {$modis[3]=45;}
	if(($modis[3]>53)&&($modis[3]<=60)) {$modis[3]=0;}
	if(($modis[3]>=0)&&($modis[3]<=8)) {$modis[3]=0;}

	$tem1=$modis[4];
	$modis[4]=$modis[12];    ##longitude is replaced using GOES E imager 
	$modis[12]=$tem1; ##original LON
	$tem1=$modis[5];
	$modis[5]=$modis[13];    ##latitude is replaced using GOES E imager 
	$modis[13]=$tem1; ##original LAN
	$origlc=$modis[10];
	$lc=0;
	&LCTYPE;

	if($modis[11]==999)  #change time for MODIS AVHRR
	{
	    if($lc==1){$modis[8]=$ModAVF;} #forest
	    else{$modis[8]=$ModAVNF;} # NON forest
	    if($lc==0) {$modis[8]=$ModAV;}
	}
	for($i2=4;$i2<$m2;$i2++)   ##reformat
	{
	    $modis[$i2]=sprintf("%.3f",$modis[$i2]);
	}

	$modisnew[$k]=join('',$modis[1],$modis[4],$modis[5]);  ##day GOES lon and Lan
	$modisnew[$k].=", ",
	$modisnew[$k].=join(', ',@modis);
	$k++;
    }


    $modisnew[$k]="day_lanlog, year, day, hour, min, GOESlon, GOESlan, Satzen, pixelsize, FireSize, Temp, Eco, Flag, longitude, latitude, dlon, dlan"; 
    $k++;

    &time_check('blend MODIS AVHRR and GOES fire data successfully');    #Prints time and status to log file  
}




###############################################################################
# Reclass land cover types from USGS to forest, savanna, shrub, grass and croplands.
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 July 2011
###############################################################################


sub LCTYPE{
    $lc=0;
    if(($origlc>=3)&&($origlc<=6))
    {
	$lc=1;
    }
    if(($origlc>=18)&&($origlc<=29))
    {
	$lc=1;
    }
    if(($origlc>=33)&&($origlc<=34))
    {
	$lc=1;
    }
    if($origlc==54)
    {
	$lc=1;
    }
    if(($origlc>=56)&&($origlc<=57))
    {
	$lc=1;
    }
    if(($origlc>=61)&&($origlc<=62))
    {
	$lc=1;
    }
    if(($origlc>=77)&&($origlc<=79))
    {
	$lc=1;
    }
    if($origlc==90)
    {
	$lc=1;
    }
    if(($origlc==13)||($origlc ==32)||($origlc==43))
    {
	$lc=2;
    }
    if(($origlc>=47)&&($origlc<=48))
    {
	$lc=2;
    }
    if($origlc==55)
    {
	$lc=2;
    }
    if(($origlc>=58)&&($origlc<=60))
    {
	$lc=2;
    }
    if($origlc==91)	
    {
	$lc=2;
    }
    if(($origlc>=8)&&($origlc<=9))
    {
	$lc=3;
    }
    if($origlc==11)
    {
	$lc=3;
    }
    if(($origlc>=16)&&($origlc<=17))
    {
	$lc=3;
    }
    if(($origlc>=40)&&($origlc<=41))
    {
	$lc=3;
    }
    if($origlc==46)
    {
	$lc=3;
    }
    if(($origlc>=50)&&($origlc<=53))
    {
	$lc=3;
    }
    if(($origlc==64)||($origlc==63)||($origlc==87))
    {
	$lc=3;
    }

    if(($origlc>=42)&&($origlc<=44))
    {
	$lc=4;
    }
    if(($origlc==2)||($origlc==7)||($origlc==10)||($origlc==42)||($origlc==94))
    {
	$lc=4;
    }
    if(($origlc>=30)&&($origlc<=31))
    {
	$lc=5;
    }
    if(($origlc>=35)&&($origlc<=39))
    {
	$lc=5;
    }
    if(($origlc>=92)&&($origlc<=93))
    {
	$lc=5;
    }
   
}



###############################################################################
# Generate diurnal cycle of fire observations
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 July 2011
###############################################################################


sub ALLfire_fit_DUR_LONLAN{
    $infile=$GoesHmsCluster;  
    $outfilefit=$GoesHmsFited; 
    $newerr=0;

    open (TILEID, "<$ENV{WORK}/$infile") or $newerr=1;
    @fileso=<TILEID>;
    close TILEID;
    &time_check("could not open file: $infile") if $newerr;
    $newerr=0;

    $tempsize=@fileso;
    if($tempsize>250000){
	&time_check("too many fire observations not to be able to generate grid. see: goes_hms1km.c")
    }

    @modisnew=NULL;
    &AllClusterLONLAN;

    @files= sort {(split ', ', $a)[0] cmp (split ', ', $b)[0]}@modisnew; ##July 2007

    open(STANDARD, "<$StandardCurve") or $newerr=1; 
    @standfit=<STANDARD>;
    close STANDARD;
    &time_check("could not open file: $StandardCurve") if $newerr;
    $newerr=0;
    open (OUT, ">$ENV{WORK}/$outfilefit");   
   print OUT "Lon Lan YYYY DOY half_hour_fire_size(48) LC pixelsize MODIS_or_GOES\n";

    &fit_fire;
    close OUT;
    &time_check('reconstruct diurnal pattern successfully');    #Prints time and status to log file  

}

###############################################################################
#Process the fitting diurnal cycle of fire observations
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 July 2011
###############################################################################


sub  fit_fire{
    $total=0;
    $kk=0;
    $oldday=0;
    $i=0;
    $j=0;
    $n=0;
    $totalnn=0;
    $n1545=0;
    $totalnn1545=0;
    $mod=0;

    $MODsize=0.122;  ##modis_AVHRR fire size
    $MODsizeF=0.245;  ##modis_AVHRR fire size forest
    $MODsizeNF=0.288;  ##modis_AVHRR fire size Non forest
  
    $ModAV=0.128;  
    $ModAVF=0.1154;  
    $ModAVNF=0.159;  

    @pixel=NULL;

    @dlc=NULL;
    foreach $tile (@files){
	chomp($tile);
	$kk++;
	@data=split(', ',$tile);
	if($data[0] eq $oldday)  #get fir in the same day and same location
	{
	    $pixel[$j]=$tile;	    
	    if(($data[12]<3)||($data[12]==999))  ##8/2011
	    {
		$n++;
	    }

	    if(($data[12]<5)||($data[12]==999))   ##  exclude low possibility
	    {
		$n1545++;
	    }
	    if(($data[12]<6)||($data[12]==999))   ## includes low possibility
	    {
		$totalnn++;
		$totalnn1545++;
	    }
	    
	    $j++;
	}else{	##one GOES pxiel

	    &FIRELST2UTC; ##convert LST of representative fires to UTC for curve fitting
 
	    for($k1=0;$k1<48;$k1++)
	    {
		$firesize[$k1]=-100;  #intial values
		$adfiresize[$k1]=-100;
		$foireststd[$k1]=0;
	    }

	    $y=0;
	    for($k1=0;$k1<100;$k1++)
	    {  
		$dlc[$k1]=0;
	    }
	    @datap=NULL;
	    $MaxPixel=500;  ##pixel size with a intial value, 4*4km
	    $MODIS=0;
	    $GOES=0;
	    for($j1=0;$j1<$j;$j1++)
	    {
		@datap=split(', ',$pixel[$j1]);
		
		if($datap[12]==999) {$MODIS=999;}
		if($datap[12]!=999) {$GOES=999;}
	    }


	    if(($MODIS==999)&&($GOES!=999))  #No fitting for MODIs and AVHRR only
	    {
              ###calculate pixel size dymanically 
	    $R=6371.1; ##*radius of the Earth, km**
	    $pi=3.141596;
	    $lon=abs($datap[5]*$pi/180);
	    $lan=abs($datap[6]*$pi/180);
	    $dlon=abs($datap[15]*$pi/180);
	    $dlan=abs($datap[16]*$pi/180);
	    $lo=$R*$dlon*cos($lan);
	    $la=$R*$dlan;
	    $MaxPixel=$lo*$la;

		&MODISAVHRRpix;

	    }else{   ###for GOES and MODIS fires

		$MaxPixel=99;  ##initial value
		@datap=NULL;
		for($j1=0;$j1<$j;$j1++)
		{
		    @datap=split(', ',$pixel[$j1]);	      
		    if(($MODIS==999)&&($GOES==999)&&($datap[12]==999)){
			$datap[9]=-9;
		    }  #MODIS and AVHRR without fire size

		    if(($datap[4]>=0)&&($datap[4]<30))   #GOES E_W
		    {
			$h=2*$datap[3];
		    }
		    if(($datap[4]>=30)&&($datap[4]<60))  #GOES E_W
		    {
			$h=2*$datap[3]+1;
		    }
               ## there may be two observations
		    if(($firesize[$h]>0)&&($datap[9]>0))
		    {
			$firesize[$h]=($firesize[$h]+$datap[9])/2.0;  #get 48 values
		    }else{
			if($firesize[$h]<=0){
			    $firesize[$h]=$datap[9];  #get 48 values
			}
		    }


		    if(($MaxPixel>$datap[8])&&($datap[8]>1)) 
		    {$MaxPixel=$datap[8];}  ##selecte the smallest pixel size in GOES pixels
		    $origlc=$datap[11];   ##ecosystem type
		    $dlc[$origlc]=$dlc[$origlc]+1; #get majority LC
		} #for


#get majority LC
		$nlc=0;
		for($k1=1;$k1<100;$k1++)
		{  
		    if($dlc[$k1]>$nlc)
		    {
			$origlc=$k1;
			$nlc=$dlc[$k1];
		    }
		}


##9/28/06 time period
		$start1=-9;  ##fire begining
		$end1=-9;   ##fire ending
		$duration=0;

		for($k1=0;$k1<48;$k1++) 
		{
		    if(($firesize[$k1]>-10)&&($start1<0))
		    {
			$start1=$k1;
		    }
		}
		for($k1=47;$k1>=0;$k1--) 
		{
		    if(($firesize[$k1]>-10)&&($end1<0))
		    {
			$end1=$k1;
		    }
		}
		$duration=$end1-$start1;

		&LCTYPE;   ##get representative ecosystems 0-4 forest, savana, shrub, grass, crop	
		$lc=$lc-1;

##################################################################################################

##long gap should not be continued
##################################################################################################
##2012 feb

		$t1=0;
		$t2=0;
		$t3=0;
		$t4=0;

		if($lc==0){
		    $y2=2;  ##extension time for forest
		}else{
		    $y2=3;  #extension time for non forest
		}
		
		if((($duration>8)&&(($totalnn>6)||($n1545>4)))||(($n>1)&&($n1545>4)))   ###2012 feb	    
		{
		    if($lc==0){
			$y2=2;    ## 1 hour
		    }else{
			$y2=3;    ##1.5 hour
		    }
		}else{
		    if((($duration>6)&&($totalnn>3))||($n>0)||($n1545>1)){
			$y2=1;  ##0.5 hour
		    }
		}

		@x=NULL;
		for($k1=0;$k1<48;$k1++) 
		{
		    $x[$k1]=0.001;  
		}
		for($k1=0;$k1<48;$k1++) 
		{	
		    if($firesize[$k1]>-10)
		    {	
			$t3=$k1-$y2;
			$t4=$k1+$y2;			

			if(($t4<48)&&($t3>0)){
			    for($k2=$t3;$k2<=$t4;$k2++){
				$x[$k2]=1;
			    } 
			}else{
			    if($t4>=48){
				for($k2=$t3;$k2<48;$k2++){
				    $x[$k2]=1;
				} 
	     
				for($k2=0;$k2<=$t4-48;$k2++){
				    $x[$k2]=1;
				} 
			    }

			    if($t3<0){				
				for($k2=48+$t3;$k2<48;$k2++){
				    $x[$k2]=1;
				} 
				for($k2=0;$k2<$t4;$k2++){
				    $x[$k2]=1;
				} 
			    }
			}
		    }
		}

#################################################################


		if($GOES==999)  #GOES appeared. No fitting for MODIs and AVHRR only
		{  #fiting
		    if(($lc>=0)&&($lc<=5))
		    {
			&FITCURVE; ## generate fire size fitting
		    }

		    if((($duration>8)&&(($totalnn>6)||($n1545>4)))||(($n>1)&&($n1545>4)))   ###2012 feb
		    {
			$subtal=0;
			$nfi=0;

			if($lc==0){
			    $end1=$end1+2;   ##1 hour
			    $start1=$start1-2;  ##1 hour
			}else{
			    $end1=$end1+3;   ##1.5 hour
			    $start1=$start1-3;  ##1.5 hour
			}

			if($end1>47) {$end1=47;}
			if($start1<0) {$start1=0;}
			for($k1=$start1;$k1<=$end1;$k1++) 
			{
			    $adfiresize[$k1]=$x[$k1]*($foireststd[$k1]+$y);
			    if($adfiresize[$k1]>0)
			    {
				$subtal=$subtal+$adfiresize[$k1];
				$nfi=$nfi+1;
			    }
			}
			for($k1=$start1;$k1<=$end1;$k1++){ 
			    if(($adfiresize[$k1]<0)&&($x[$k1]==1)) 
			    {$adfiresize[$k1]=$subtal/$nfi;}
			}
		    }else{ 
	       
			if((($duration>6)&&($totalnn>3))||($n>0)||($n1545>1)){
			    $subtal=0;
			    $nfi=0;

			    $end1=$end1+1;   ##0.5 hour
			    $start1=$start1-1;  ##0.5 hour
				        #	$y=0;
			    if($end1>47) {$end1=47;}
			    if($start1<0) {$start1=0;}

			    for($k1=$start1;$k1<=$end1;$k1++) 
			    {
				$adfiresize[$k1]=$x[$k1]*($foireststd[$k1]+$y);
				if($adfiresize[$k1]>0)
				{
				    $subtal=$subtal+$adfiresize[$k1];
				    $nfi=$nfi+1;
				}
			    }
			    for($k1=$start1;$k1<=$end1;$k1++){ 
				if(($adfiresize[$k1]<0)&&($x[$k1]==1)) 
				{$adfiresize[$k1]=$subtal/$nfi;}
			    }				    
			}
		    }  ##else       
		} #end  calculation of GOES=9999

		$subtal=0;
		for($k1=0;$k1<48;$k1++)  #addressed fire size
		{
		    if($adfiresize[$k1]>0)
		    {$subtal=$subtal+$adfiresize[$k1];}
		}
 
		$outd= join(' ',$datap[5], $datap[6], $datap[1], $datap[2]);
		for($k1=0;$k1<48;$k1++)  #addressed fire size
		{
		    $pixelsize=0.40*$MaxPixel;  ##2012
		    
		    if($subtal>$pixelsize) #proxy data --pixel size from fire product
		    {
			if($adfiresize[$k1]>0){  ##04/2011
			    $adfiresize[$k1]=$adfiresize[$k1]*$pixelsize/$subtal;
			}
		    }
		    if($adfiresize[$k1]>0)
		    {
			$total=$total+$adfiresize[$k1];
		    }

		    $outd.=sprintf(" %.4f", $adfiresize[$k1]);
		}

		if($subtal>$pixelsize)
		{
		    $subtal=$pixelsize;
		}
	   
		if(($oldday==0)||($oldday=~m/day/))    ##4/19/07
		{
		    $oldday=0;
		}else{
		    $lc=$lc+1;

		    if($subtal>0){

##convert representative time to UTC time
			$modisavhrr=0;  ###GOES foot print
			if(($subtal<0.8*$MaxPixel)&&($subtal>0)){  
			    print OUT "$outd $subtal $origlc $MaxPixel $modisavhrr\n";  
			}
		    }
		}
	    } #####for GOES and MODIS fires

	    @dlc=NULL;
	    @pixel=NULL;
	    $j=0;
	    $n=0;
	    $totalnn=0;
	    $n1545=0;
	    $totalnn1545=0;
	    $mod=0;
	    $pixel[$j]=$tile;

	    if(($data[12]<3)||($data[12]==999))  ##  exclude low possibility  999--MODIS_AVHRR
	    {
		$n++;
	    }

	    if(($data[12]<5)||($data[12]==999))  ##  exclude low possibility  999--MODIS_AVHRR
	    {	
		$n1545++;
	    }
	    if(($data[12]<6)||($data[12]==999))  ## includes low possibility
	    {
		$totalnn++;
		$totalnn1545++;
	    }
	    $j++;
	    $oldday=$data[0];
	} #else--one GOES pixel

    }  #foreach
    
}



##############################################################################
## generate fire sizes by fitting fire data to a representive fire curves
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub FITCURVE{
    $b=0;
    $bs=0;
    $bc=0;
    $a=0;
    $st1=0;
    $st2=0;
    $bb=0;

    for($k1=0;$k1<48;$k1++)
    {
	@firelc=split(' ',$standfitUTC[$k1]);  #get standard (representative) fitting values

	$foireststd[$k1]=$firelc[$lc];
	if($lc==5)
	{  ##all modis_avhrr using average
	    $foireststd[$k1]=($firelc[0]+$firelc[1]+$firelc[2]+$firelc[3]+$firelc[4])/5.0;
	}

	if($firesize[$k1]>0)
	{
	    $b=$firesize[$k1]-$foireststd[$k1];
	    $bs=$bs+$b*$b;
	    $bc=$bc-2*$b;
	    $a=$a+1;
	    $bb=$bb+$b;
	}
    }
    if((($a>2)&&($lc<3))||(($a>1)&&($lc>=3)))
    {
	$bbcc=$bc*$bc-4*$a*$bs;
	if($bbcc>0)
	{
	    $y1=(-$bc+sqrt($bbcc))/(2*$a);
	    $y2=(-$bc-sqrt($bbcc))/(2*$a);
	    for($k1=0;$k1<48;$k1++)  #determine the y value for shift
	    {
		if($firesize[$k1]>0)
		{
		    $st1=$st1+($firesize[$k1]-$foireststd[$k1]-$y1)*($firesize[$k1]-$foireststd[$k1]-$y1);
		    $st2=$st2+($firesize[$k1]-$foireststd[$k1]-$y2)*($firesize[$k1]-$foireststd[$k1]-$y2);
		}
	    }
	    $y=$y1;
	    if($st1>$st2)
	    {
		$y=$y2;
	    }
	}else{  ##$bbcc<0
	    $y=$bb/$a;
	}
    }else{
	$y=0;
    }
}





###############################################################################
# 
#Convert fire size with LST to UTC time and then using the data to fit with real UTC fire data
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub FIRELST2UTC{
    $lon=(split(', ',$pixel[0]))[5];
    $min=4*$lon;  ##minute

    for($k9=0;$k9<48;$k9++)
    {
	$UTC=$k9*30-$min;      ##$k9*30 as solar local time, $min is negative in the USA
	$UTC=($UTC+15)/30.0;    ##15 minute as a buffer
	if($UTC>47.9) {$UTC=$UTC-48;}
	$t=(split('\.', $UTC))[0];
	$standfitUTC[$t]=$standfit[$k9];
    }

###start from prehour--12/22/2010
    $lcsth=2*$prehour;
    @stutc=NULL;
    $t=0;
    for($k9=$lcsth; $k9<48;$k9++)
    {
	$stutc[$t]=$standfitUTC[$k9];
	$t++;
    }

    for($k9=0; $k9<$lcsth;$k9++)
    {
	$stutc[$t]=$standfitUTC[$k9];
	$t++;
    }

    @standfitUTC=NULL;
    @standfitUTC=@stutc;   ###the standfitUTC is compariable with representive time
}



###############################################################################
# 
# Get GOES fire pixels from FTP sites  
# the day is the previous day and the time is the representive time (0-23.5) in the second and third columns
# the real year, day and time are located at the last three columns. 
#
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################


sub getGOES{
    my @files=NULL;

    $newerr=0;

#######################get ABBA data#########
#-- On Oct.28 2015, retrival ABBA filt data is changed from sftp from satepsanon to copy from SFS
 
#    $localDir='/home/oper/BBEP2/ABBA_DIR';

###    $remoteDir='/data/ftp/pub/FIRE/ABBA';
#    $remoteDir='/data/ftp/pub/FIRE/forPo';
#    chdir $localDir;

#    open(SFTP,'|sftp oper@satepsanon.nesdis.noaa.gov');
#    print SFTP "cd $remoteDir\n";
#    print SFTP "lcd $localDir\n";

#    print SFTP "mget $filename0*filt\n";
#    print SFTP "mget $filename*filt\n";
#    print SFTP "quit\n";
#    close(SFTP);

    $FiltDir=join('',$ENV{INDATA},"ABBA");
    unlink (glob("$FiltDir/*.filt")) or warn "can't delete files: $!";

#--sftp abba filt data from satepsanon for testing 
$remoteDir='/data/ftp/pub/FIRE/forPo';
open(SFTP,'|sftp oper@satepsanon.nesdis.noaa.gov');
print SFTP "cd $remoteDir\n";
print SFTP "lcd $FiltDir\n";
print SFTP "mget $filename0*filt\n";
print SFTP "mget $filename*filt\n";
print SFTP "quit\n";
close(SFTP);


#--retrieval ABBA filt data from SFS
#    print "copy ABBA filt data\n";
#    system("cp $ENV{ABBA_SFS_DIR}/$filename0*filt $FiltDir");
#    system("cp $ENV{ABBA_SFS_DIR}/$filename*filt $FiltDir");

    chdir $FiltDir;

#######################get ABBA data#########


    my @fil1=NULL;
    my @fil2=NULL;
    @fil1=<$filename0*filt>;
    @fil2=<$filename*filt>;
    @files=(@fil1,@fil2);

############


##file name is composed of current daya nd previous Hour, indicating all fires before this time
	$outfile=$filename0;

    if($prehour==0){
	$outfile.="00";
    }
    if(($prehour>0)&&($prehour<10)){
	$outfile.="0";
	$outfile.=$prehour;
    }
    if($prehour>=10){
	$outfile.=$prehour;
    }


    $outfile.="_day";
    $outfile.="_fire_V65";
    $outfile.=".txt";

    @fileday=NULL;
    $i=0;

    foreach $fileline (@files) {
##	$filen=(split(' ', $fileline))[8];
	$filen=$fileline;
################# GOES  ###########################
 
	if(($filen=~m/^f([0-9]{7})([0-9]{2})([0-9]{2}).v65.g15.filt$/)||($filen=~m/^f([0-9]{7})([0-9]{2})([0-9]{2}).v65.g13.filt$/))
	{
	    $data='f';
	    $data.=$1;
	    $fhour=$2;

	    if(($data eq $filename)&&($fhour>=$prehour))
	    {
		$fileday[$i]=$filen;
		$i++;
	    }

	    if(($data eq $filename0)&&($fhour<$hour))
	    {
		$fileday[$i]=$filen;
		$i++;
	    }
	}
	    ####
    }  ##end files

##############################################

    $outfileGO=$outfile;

    open(OUT2,">$ENV{WORK}/$outfile"); 
#    open(OUT2,">$FiltDir/$outfile");




##print OUT2 "RepYear, RepDay, RepHour, min, longitude, latitude, T4, T11, Size, Temp, Eco, Flag, Year, Day, Hour\n";
 
    print OUT2 "RepYear, RepDay, RepHour, min, ";
    print OUT2 "Longitude, Latitude, Satzen(deg), Pix Size(km2), T4(K), T11(K), FireSize(km2), Temp(K), FRP(MW), Ecosystem, Flag, Year, Day, Hour\n";




#print "Getting file\n";

    if($#fileday>2){  ##JUly 2010 in case not data available  
	foreach $stuff(@fileday){
 
 ##          print "file===$stuff\n";


	    &firedata;

	    system("rm $stuff");

	}
    }
    close(OUT2);
 

 chdir("$ENV{WORK}");   
  ##  print "end in getting files\n";

     &time_check('get GOES fire successfully');    #Prints time and status to log file  
}







###############################################################################
#  Arrange GOES fire data
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################


sub firedata{
    my	$year1=0;
    my	$day1=0;
    my	$hour1=0;
    my	$min1=0;
    $newerr=0;

    open(HALFF,"<$stuff") or  $newerr=1;
    @hlaf_fire=<HALFF>;
    close HALFF;
    &time_check("could not open file: $stuff") if $newerr;

    $newerr=0;
     ####year, day, hour, min, longitude, latitude, T4, T11, Size, Temp, Eco, Flag
    @yeardate=split ('\.',$stuff);

    if($yeardate[0]=~m!(f([0-9]{4})([0-9]{3})([0-9]{2})([0-9]{2}))$!)
    {
	$y2=$2;
	$d2=$3;
	$h2=$4;
	$min1=$5;
    }

     #### Generate representative day and hour##########
    $data=join('', "f",$y2,$d2);
 
    if(($data eq $filename)&&($h2>=$prehour))
    {
	$hour1=$h2-$prehour;
	$year1=$y2;
	$day1=$d2;
    }

    if(($data eq $filename0)&&($hour1<$hour)){
	$hour1=24-$prehour+$h2; 
	$year1=$preyear;  
	$day1=$preyday;   
    }
		  

    $num=0;
    foreach $line(@hlaf_fire)
    {
	chomp($line);

	$num=$num+1;
	if($num>5)  ##for filt fires
	{
	    $newline=join (" ", $year1, $day1, $hour1, $min1);
	    $newline2=join (" ",$newline,$line,$y2,$d2, $h2);
	    @lindata=split(" ", $newline2);

	    if($lindata[5]>$Lamin)  ##NA latitude 03/01/2012
	    {
		$i=0;
		foreach $data(@lindata)
		{
		    chomp($data);
		    $data=$data*1;    #to omit the space
		    $tt=$data;
		    if($data=~m/^\./)
		    {
			$lindata[$i]=0;
			$lindata[$i].=$tt;
		    }
		    if($data=~m/\.$/)
		    {
			$lindata[$i]=$tt;
			$lindata[$i].=0;
		    }
		    $i++;
		}
		$newline2=join ", ",@lindata;
		print OUT2 "$newline2\n";
	    }
	}
    }  
}

###############################################################################
# get time of current and previous day, month and year
#
#        Developers: Xiaoyang Zhang
#                    ERT 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";
    ##6 hour interval--running every 6 hour###
    $hourt=0;
    if(($hour>=6)&&($hour<12)){
	$hourt=6;  
	$prehour=6;
    }

    if(($hour>=12)&&($hour<18)){
	$hourt=12;  
	$prehour=12;
    }

    if(($hour>=18)&&($hour<24)){
	$hourt=18;  
	$prehour=18;
    }

    if(($hour>=0)&&($hour<6)){
	$hourt=0;  
	$prehour=0;
    }

    $hour=$hourt;
    $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;
	}
    }



    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;
	}

    }


   ###GOES
   ##current fYYYYDDDHH.
    $filename0=$curyear;
    if(($curyday>=1)&&($curyday<10))
    {
	$filename0.="00";
	$filename0.=$curyday;
    }
    if(($curyday>=10)&&($curyday<100))
    {
	$filename0.="0";
	$filename0.=$curyday;
    }
    if(($curyday>=100)&&($curyday<400))
    {
	$filename0.=$curyday;
    }

    $yydd=$filename0;
    $filename0="f";
    $filename0.=$yydd;

##previous fYYYYDDDHH.
##$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;

##current fYYYYDDDHH.
    $hmsfilename0="hms";
    $hmsfilename0.=$curyear;

    if($curmon<10) {$hmsfilename0.="0";}
    $hmsfilename0.=$curmon;
    if($mday<10) {$hmsfilename0.="0";}
    $hmsfilename0.=$mday;
    $hmsfilename0.=".prelim.txt";  ##01042010

#####previous day########
    $hmsfilename="hms";
    $hmsfilename.=$preyear;

    if($premon<10) {$hmsfilename.="0";}
    $hmsfilename.=$premon;
    if($premday<10) {$hmsfilename.="0";}
    $hmsfilename.=$premday;
    $hmsfilename.=".prelim.txt";  ##01042010

#####file yyyymmddhh
   $dayf=$curyday;

    if($hour==0){
	$sth=18;
	$dayf=$preyday;
    }
    if($hour==6){
	$sth=0;
    }
    if($hour==12){
	$sth=6;
    }
    if($hour==18){
	$sth=12;
    }

    $hourn=$prehour-1;
    if($hourn<0){$hourn=23;}

    $tsth=$sth;
    if($sth<10) {$tsth=join('',"0",$sth);}
    $thourn=$hourn;
    if($hourn<10) {$thourn=join('',"0",$hourn);}

    if($prehour==0){
	$tpremon=$premon;
	if($premon<10) {$tpremon=join('', "0",$premon);}
	$tpremday=$premday;
	if($premday<10) {$tpremday=join('', "0",$premday);}
	$yyyymmddhh=join('',$preyear, $tpremon, $tpremday);
	$yyyymmddhh=join('_',$yyyymmddhh,$tsth,$thourn);

    }else{
	$tcurmon=$curmon;
	if($curmon<10) {$tcurmon=join('', "0",$curmon);}
	$tmday=$mday;
	if($mday<10) {$tmday=join('', "0",$mday);}
	$yyyymmddhh=join('',$curyear, $tcurmon, $tmday);
	$yyyymmddhh=join('_',$yyyymmddhh,$tsth,$thourn);
    }
}



###############################################################################
# Get hms data from FTP site
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#                 July 2011
###############################################################################




#######2012
sub getHMS {

$hmsf=(split('\.',$hmsfilename))[0];
$hmsf.=".txt";
#GGGG$hmsf.=".prelim.txt";
$hmsf0=(split('\.',$hmsfilename0))[0];
$hmsf0.=".txt";
#GGGG$hmsf0.=".prelim.txt";


#######################get HMS data#########
#$localDir='/home/oper/BBEP2/HMS';
$localDir='/data/Pbbep/BBEP2/data/HMS';
$remoteDir='/data/ftp/pub/FIRE/HMS';
chdir $localDir;

unlink (glob("$localDir/*.txt")) or warn "can't delete files: $!";

open(SFTP,'|sftp oper@satepsanon.nesdis.noaa.gov');
print SFTP "cd $remoteDir\n";
print SFTP "lcd $localDir\n";

print SFTP "get $hmsfilename\n";
print SFTP "get $hmsfilename0\n";
print SFTP "get $hmsf\n";
print SFTP "get $hmsf0\n";

print SFTP "quit\n";
close(SFTP);
#######################get HMS data#########

###HMS *prelim.txt may not be completed 
### if hms*.txt exists, it is used to replace *prelim.txt 

$hmsfsize=0;
$hmsfsizepre=0;

if(-e $hmsf){
$hmsfsize = -s $hmsf;
}
if(-e $hmsfilename){
$hmsfsizepre = -s $hmsfilename;
}

if($hmsfsize>$hmsfsizepre){
    if((-e $hmsfilename)&&(-e $hmsf)) { 
unlink($hmsfilename);

rename($hmsf,$hmsfilename);
}
}
##################
$hmsfsize=0;
$hmsfsizepre=0;

if(-e $hmsf0){
$hmsfsize = -s $hmsf0;
}
if(-e $hmsfilename0){
$hmsfsizepre = -s $hmsfilename0;
}

if($hmsfsize>$hmsfsizepre){
    if((-e $hmsfilename0)&&(-e $hmsf0)) { 
unlink($hmsfilename0);

rename($hmsf0,$hmsfilename0);
}
}


    $newerr=0;	
    &Daily_hms_extract_fire_time;
 
system("mv $hmsfilename2d $ENV{WORK}/.");

chdir("$ENV{WORK}");

   #Prints time and status to log file  
     &time_check('get HMS files successfully');    #Prints time and status to log file   
}






###############################################################################
# move all outputs to the output directory
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub mvoutput{   
      
    system("mv $jpgfileNA $ENV{OUTPUT}/");
    system("mv $cdffile $ENV{OUTPUT}/");
    system("mv $jpgfileqa $ENV{OUTPUT}/");
 
    if(-e $grib2_burn1km){
	system("gzip $grib2_burn1km");

	$grib2_burn1km2=$grib2_burn1km;
	$grib2_burn1km2.=".gz";
	system("mv $grib2_burn1km2 $ENV{OUTPUT}/");
    }
    if(-e $grib2_burn12km){

	system("gzip $grib2_burn12km");
	$grib2_burn12km2=$grib2_burn12km;
	$grib2_burn12km2.=".gz";
	system("mv $grib2_burn12km2 $ENV{OUTPUT}/");
    }

    $outfilehalfhPM25BAtxt=$outfilehalfhPM25BA;
    $outfilehalfhPM25BAtxt.=".txt";

    if(-e $outfilehalfhPM25BA) {
	system("mv $outfilehalfhPM25BA $outfilehalfhPM25BAtxt");
	system("mv $outfilehalfhPM25BAtxt $ENV{OUTPUT}/");
    }



    if(-e $GOESdaily) {unlink ($GOESdaily);}
    if(-e $hmsfilename2d) {unlink ($hmsfilename2d);}
    if(-e $outfilehalfhPM25BA) {unlink ($outfilehalfhPM25BA);}
    if(-e $outfile) {unlink ($outfile);}
    if(-e $GoesHmsFited) {unlink ($GoesHmsFited);}
    if(-e  $outputemissionall) {unlink ($outputemissionall);}
    if(-e $outfilelanlon) {unlink ($outfilelanlon);}
    if(-e $hmsfilename) {unlink ($hmsfilename);}
    if(-e $hmsfilename0) {unlink ($hmsfilename0);}

}


###############################################################################
# get weekly VCI for fuel moisture,  16km data
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub vcidata{
    $year1=0;
    $doy1=0;

    if($wday>1){
	$doy1=$curyday-7-$wday+1;  ## get the DOY for Monday
    }else{
	$doy1=$curyday-2*7-$wday+1;  ## get the DOY for Monday
    }

    if($curyear=~m/([0-9]{2})([0-9]{2})/)
    {
	$year1=$2;
    }


    if($doy1<=0){
	$year3=$curyear-1;
	if(($year3%4==0)&&($year3%100!=0))   ##leap year
	{
	    $doy1=$doy1+366;
   	}else{
	    $doy1=$doy1+365;
	}

	if($year3=~m/([0-9]{2})([0-9]{2})/)
	{
	    $year1=$2;
	}
    }


    if($doy1<10)
    {
	$doy1=join('',"00",$doy1);
    }
    if(($doy1>=10)&&($doy1<100))
    {
	$doy1=join('',"0",$doy1);
    }

    $vcifilestem="NPR.VGWG.NN.D"; 
    $filevcif1=join('',$vcifilestem,$year1,$doy1, ".VCII");

    &ftp_VCI_files;

####get VCI from ftp

    $outfile1= "$ENV{WORK}/VCI_temp";  ## a temporary file
    if(-e $outfile1){
	unlink ($outfile1);
    }


  ##### processing HDF2BIN;

    if(-e "$filevcif"){$newerr=0;}
    else{&time_check("file does not exist: $filevcif");}

    system("$ENV{SOURCE}/GVI_subset_NA.exe $filevcif $outfile1");

##processing repojection;
    if(-e "$ENV{WORK}/VCI_Y_LA.out"){
	unlink ("$ENV{WORK}/VCI_Y_LA.out");  ## a temporary file
    }


    if(-e "$ENV{SOURCE}/reproj_linux_v2.exe"){$newerr=0;}
    else{&time_check("file does not exist:reproj_linux_v2.exe");}
#    if(-e "$ENV{SOURCE}/CONvert_VCI_Y_NA.in"){$newerr=0;}
    if(-e "$ENV{STINPUT}/CONvert_VCI_Y_NA.in"){$newerr=0;}
    else{&time_check("file does not exist:CONvert_VCI_Y_NA.in");}
#    if(-e "$ENV{SOURCE}/CONvert_VCI_Y_NA_2km.out"){$newerr=0;}
    if(-e "$ENV{STINPUT}/CONvert_VCI_Y_NA_2km.out"){$newerr=0;}
    else{&time_check("file does not exist:CONvert_VCI_Y_NA_2km.out");}
 
  #  system("$ENV{SOURCE}/reproj_linux_v2.exe $ENV{SOURCE}/CONvert_VCI_Y_NA.in $ENV{SOURCE}/CONvert_VCI_Y_NA_2km.out");  ##from lan/lon to Lambert projection
    system("$ENV{SOURCE}/reproj_linux_v2.exe $ENV{STINPUT}/CONvert_VCI_Y_NA.in $ENV{STINPUT}/CONvert_VCI_Y_NA_2km.out");

    $outfilevci=join('', $vcifilestem, $year1,$filevci,".LA_NA");

    if(-e "$ENV{SOURCE}/subsetLA_NA16_2km.exe"){$newerr=0;}
    else{&time_check("file does not exist:subsetLA_NA16_2km.exe");}
    if(-e "VCI_Y_LA_NA2km.out"){$newerr=0;}
    else{&time_check("file does not exist: VCI_Y_LA_NA2km.out");}
    system("$ENV{SOURCE}/subsetLA_NA16_2km.exe VCI_Y_LA_NA2km.out $outfilevci");
    &time_check('create VCI in NA successfully');    #Prints time and status to log file  
}


##############################################################################
#  
#      get VCI from ftp  site
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################


sub ftp_VCI_files{

########################################
## get the VCI data satepsdist4

#$localDir='/home/oper/BBEP2/VCI_DIR';
$localDir='/home/Pbbep/BBEP2/VCI_DIR';
$remoteDir='/data/oper/data1/ftp/pub/VCII';
chdir $localDir;

open(SFTP,'|sftp oper@satepsdist4.nesdis.noaa.gov');
print SFTP "cd $remoteDir\n";
print SFTP "lcd $localDir\n";
print SFTP "mget NPR.VGWG.NN.*VCII\n";

print SFTP "quit\n";
close(SFTP);


@filesvci=<NPR.VGWG.NN.*VCII>;
    
    $tx1=-9;
    foreach $filen (@filesvci) {
	chmod($filen);
	if($filen=~m/.VCII/)
	{
	    $filevcif2=$filen;
	    if($filevcif1 eq $filen)
	    {
		$tx1=0;
	    }
	} 
    }
    
    $filevcif=$filevcif2;
    if($tx1==0) {$filevcif=$filevcif1;}

 $filevcif=join('/', $localDir,$filevcif);   
##    $filevcif=join('/', $ENV{VCI_DIR},$filevcif);
chdir("$ENV{WORK}");

} 





################
####################
###############################################################################
# get weekly VCI for fuel moisture 4km
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

  sub vcidata_local{
    $year1=0;
    $doy1=0;
    $doy1=$preyday/7;
    $daype=sprintf("%d",$doy1);
    $year1=$preyear;

    if($daype==0)
    {
	$year1=$preyear-1;  #last week in previous year is used for the first week of the year
	$daype=52;
    }
    $daype2=$daype-1;
    $year2=$year1;
    if($daype2==0)
    {
	$year2=$year2-1;  #last week in previous year is used for the first week of the year
	$daype2=52;
    }

    $daype3=$daype2-1;
    $year3=$year2;
    if($daype3==0)
    {
	$year3=$year3-1;  #last week in previous year is used for the first week of the year
	$daype3=52;
    }

##get AVHRR weekly VCI data with file name *hdf
    $vcifilestem="GVIX_NN_G04_C07_VCI_Y"; 
    if($daype<10)
    {
	$fvci="_P0";
    }else{
	$fvci="_P";
    }
    $fvci.=$daype;
    $filevcif1=join('',$vcifilestem,$year1,$fvci, ".hdf");

##VCI in 1 week before
    if($daype2<10)
    {
	$fvci="_P0";
    }else{
	$fvci="_P";
    }
    $fvci.=$daype2;
    $filevcif2=join('',$vcifilestem,$year2,$fvci, ".hdf");

##VCI in 2 week before
    if($daype3<10)
    {
	$fvci="_P0";
    }else{
	$fvci="_P";
    }
    $fvci.=$daype3;
    $filevcif3=join('',$vcifilestem,$year3,$fvci, ".hdf");

    &ftp_VCI_files_local;

####get VCI from ftp

    $outfile1= "$ENV{WORK}/VCI_temp";  ## a temporary file
    if(-e $outfile1){
	unlink ($outfile1);
    }


    ##    system("$ENV{SOURCE}/GVI_HDF2bin.exe $filevcif $outfile1");
    system("$ENV{SOURCE}/GVI_HDF2bin_NA.exe $filevcif $outfile1");  


     ##processing repojection;
    if(-e "$ENV{WORK}/VCI_Y_LA.out"){
	unlink ("$ENV{WORK}/VCI_Y_LA.out");  ### a temporary file
    }

    system("$ENV{SOURCE}/reproj_linux_v2.exe  CONvert_VCI_Y_NA.in CONvert_VCI_Y_NA_2km.out");  ##from lan/lon to Lambert projection

    $outfilevci=join('', $vcifilestem, $year1,".LA");
    system("$ENV{SOURCE}/subsetLA_NA4_2km.exe VCI_Y_LA_NA.out $outfilevci");
}


##############################################################################
#  
#      get VCI from ftp  site  1km
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub ftp_VCI_files_local{

    my $host=$ENV{VCI_HOST};
    my $directory=$ENV{VCI_DIR};
    my $newerr=0;

    use Net::FTP;
    $ftp=Net::FTP->new($host,Timeout=>240) or $newerr=1;

    &time_check("could not ftp to $host") if $newerr;
    $newerr=0;

    $ftp->login("ftp","apl\@") or $newerr=1;
    
    $ftp->quit if $newerr;

    &time_check("could not login to $host ") if $newerr;
    $newerr=0;
      
    $ftp->cwd($directory) or $newerr=1; 
    &time_check("could not cd to VCI") if $newerr;
    $newerr=0;

    @filesvci=$ftp->dir or $newerr=1;
    &time_check("could not get file VCI list") if $newerr;
    $newerr=0;

    $tx1=-9;
    $tx2=-9;
    $tx3=-9;
    foreach $fileline (@filesvci) {
	chmod($fileline);
	$filen=(split(' ', $fileline))[8];
	if($filen=~m/.hdf/)
	{
	    if($filevcif1 eq $filen)
	    {
		$tx1=0;
	    }
	    if($filevcif2 eq $filen)
	    {
		$tx2=0;
	    }

	    if($filevcif3 eq $filen)
	    {
		$tx3=0;
	    }
	} 
    }

    $filevcif=$filen;
    if($tx1==0) {$filevcif=$filevcif1;}
    if(($tx1==-9)&&($tx2==0)) {$filevcif=$filevcif2;}
    if(($tx1==-9)&&($tx2==-9)&&($tx3==0)) {$filevcif=$filevcif3;}

    $ftp->binary or $newerr=1;

    $ftp->get($filevcif) or $newerr=1;

    &time_check("could not get file VCI list") if $newerr;
    $newerr=0;
    $ftp -> quit;
} 





##############################################################################
#  Calculate biomass burning emissions
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub emission{
    $outascii=$outputemissionall;
    $outbin=$outputemissionall;
    $outbin.=".out_bin";
    $static_file="$ENV{STINPUT}/Static_input";
    open(STF,">$static_file");
   
    print STF "$ENV{STINPUT}/MVPFS_fuel_ave200234_NA_2km\n";
    print STF "$ENV{STINPUT}/UMD_LC_NA_mosaic_SUB_2km\n";
 ##    print STF "$ENV{STINPUT}/stateus_LAB_linux_US3\n";
    print STF "$ENV{STINPUT}/FEPS_moisture_consumption.txt\n";
    print STF "$ENV{STINPUT}/nfdr_FOFEM_emission.txt\n";
 #   print STF "$ENV{STINPUT}/nfdr_fuel_data_Cohen_gm2.txt\n";
 #   print STF "$ENV{STINPUT}/fccs_fuel_LUT.txt\n";
    print STF "$ENV{STINPUT}/FOFEM_emiss_CO.txt\n";
    print STF "$ENV{STINPUT}/FOFEM_emiss_CH4.txt\n";
    print STF "$ENV{STINPUT}/FOFEM_emiss_TNMHC.txt\n";
    print STF "$ENV{STINPUT}/FOFEM_emiss_NH3.txt\n";
    print STF "$ENV{STINPUT}/FOFEM_emiss_N2O.txt\n";
    print STF "$ENV{STINPUT}/FOFEM_emiss_NOX.txt\n";
    print STF "$ENV{STINPUT}/FOFEM_emiss_SO2.txt\n";
 ###   print STF "$ENV{STINPUT}/DATA_LA-lanlon_NA_2km\n";  10/2011
    close(STF);

    if(-e "$ENV{STINPUT}/MVPFS_fuel_ave200234_NA_2km"){$newerr=0;}
    else{&time_check("file does not exist:MVPFS_fuel_ave200234_NA_2km");}
    if(-e "$ENV{STINPUT}/UMD_LC_NA_mosaic_SUB_2km"){$newerr=0;}
    else{&time_check("file does not exist:UMD_LC_NA_mosaic_SUB_2km");}
    if(-e "$ENV{STINPUT}/FEPS_moisture_consumption.txt"){$newerr=0;}
    else{&time_check("file does not exist:FEPS_moisture_consumption.txt");}
    if(-e "$ENV{STINPUT}/nfdr_FOFEM_emission.txt"){$newerr=0;}
    else{&time_check("file does not exist: nfdr_FOFEM_emission.txt");}
    if(-e "$ENV{STINPUT}/FOFEM_emiss_CO.txt"){$newerr=0;}
    else{&time_check("file does not exist:FOFEM_emiss_CO.txt");}
    if(-e "$ENV{STINPUT}/FOFEM_emiss_CH4.txt"){$newerr=0;}
    else{&time_check("file does not exist:FOFEM_emiss_CH4.txt");}
    if(-e "$ENV{STINPUT}/FOFEM_emiss_TNMHC.txt"){$newerr=0;}
    else{&time_check("file does not exist:FOFEM_emiss_TNMHC.txt");}
    if(-e "$ENV{STINPUT}/FOFEM_emiss_NH3.txt"){$newerr=0;}
    else{&time_check("file does not exist:FOFEM_emiss_NH3.txt");}
    if(-e "$ENV{STINPUT}/FOFEM_emiss_N2O.txt"){$newerr=0;}
    else{&time_check("file does not exist:FOFEM_emiss_N2O.txt");}
    if(-e "$ENV{STINPUT}/FOFEM_emiss_NOX.txt"){$newerr=0;}
    else{&time_check("file does not exist:FOFEM_emiss_NOX.txt");}
    if(-e "$ENV{STINPUT}/FOFEM_emiss_SO2.txt"){$newerr=0;}
    else{&time_check("file does not exist:FOFEM_emiss_SO2.txt");}

     ##   if(-e "$ENV{STINPUT}/DATA_LA-lanlon_NA_2km"){$newerr=0;}
     ##  else{&time_check("file does not exist:DATA_LA-lanlon_NA_2km");}

    if(-e "$outfilefit"){$newerr=0;}
    else{&time_check("file does not exist:$outfilefit");}
    if(-e "$outfilevci"){$newerr=0;}
    else{&time_check("file does not exist:$outfilevci");}

    if(-e "$ENV{SOURCE}/pgbbep_emis2km.exe"){$newerr=0;}
    else{&time_check("file does not exist:pgbbep_emis2km.exe");}

 #   print "$outfilefit; $outfilevci; $static_file; $outascii\n";
    system("$ENV{SOURCE}/pgbbep_emis2km.exe $outfilefit $outfilevci $static_file $outascii");

    &time_check('calculate emissions successfully');    #Prints time and status to log file  
}



##############################################################################
# Calculate total burned areas in a given lan/lon
# only data in latest 6hours are used
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub lanlonBA{
    $outfilelanlon=join('',"burned_area_",$yyyymmddhh);
    $lc_statics="lc_temp";
    open (TILEID, "<$ENV{WORK}/$GoesHmsFited") or $newerr=1; 
    @totaltiles=<TILEID>;
    close TILEID;
    &time_check("could not open file: $GoesHmsFited") if $newerr;
    $newerr=0;
    $tempsize=@totaltiles;
    if($tempsize>100000)
    {
	&time_check("too many fitted burned pixels not to be able generate 1km grid: see goes_hms1km.c");
    }

    open(OUT,">$ENV{WORK}/$outfilelanlon");
    open(OUT_LC,">$ENV{WORK}/$lc_statics");
 
    if($GoesHmsFited=~m!^f([0-9]{4})([0-9]{3})([0-9]{2})!)
    {
	$year2=$1;
	$day2=$2*10/10;
	$h=$3*10/10;
    }


    if($h==0){
	if($day2==1){
	    ##   $day2=$preyday;
	    $year2=$preyear;
	}
	$day2=$preyday;
	$td="18:00-23:59 UTC";
    }

    if($h==6){
	$td="00:00-05:59 UTC";
    }
    if($h==12){
	$td="06:00-11:59 UTC";
    }
    if($h==18){
	$td="12:00-17:59 UTC";
    }

    $mon=0;
    $mmd=0;
    if($yyyymmddhh=~m!([0-9]{4})([0-9]{2})([0-9]{2})!){   ####10/2011
	$mon=$2;                           
	$mmd=$3;
    }

    print OUT "Burned area at: $td, $mon-$mmd-$year2\n";
    print OUT_LC "Burned area at: $td, $mon-$mmd-$year2\n";

    $lonp=0;
    $lanp=0;
    $dayp=0;
    $burnarea=0;

    @lcBAs=NULL;
    @lcBAm=NULL;
    @lcBAl=NULL;

    for($i=0;$i<10;$i++){
    $lcBAs[$i]=0;
    $lcBAm[$i]=0;
    $lcBAl[$i]=0;
}

    my @datba=NULL;

    foreach $line(@totaltiles)
    {
	chomp($line);
	@datba=split(' ', $line);
	$lon=$datba[0];
	$lan=$datba[1];

	$beg=0;  ##04/2011
	$end=0;
	$dur=0;
	$lc=0;
	for($i=4;$i<4+48;$i++){ ##duirng last 24 hours
	    if(($datba[$i]>0)&&($beg==0)) {$beg=$i;}
	    if($datba[$i]>0) {$end=$i;}
	}

	$dur=($end-$beg)/2;   ##half hour

	for($i=4+36;$i<4+48;$i++){ ##last 6 hours
	    if($datba[$i]>0){
	    $burnarea=$burnarea+$datba[$i];
	}
	}

	if($burnarea>0){
	    print OUT "$lon $lan $burnarea\n";
	    $lc=0;
	    ##$lc=$datba[53];  ##from ABBA
	    $origlc=$datba[53];  ##from ABBA
	    &LCTYPE;  ## get ecosystem lables 1-5

          ##     Fire number- based on ecosystem
	    if(($dur>=0)&&($dur<8)){$lcBAs[$lc]=$lcBAs[$lc]+1; }  ##short burn
	    if(($dur>=8)&&($dur<16)){$lcBAm[$lc]=$lcBAm[$lc]+1; }  ##median burn
	    if($dur>=16){$lcBAl[$lc]=$lcBAl[$lc]+1; }  ##long burn
	}
	$burnarea=0;
    }

##last ,8h, 8-16h, 16-24h

       ### print OUT_LC "large medium small fire\n"
       ### print OUT_LC "$lcBA[1]  $lcBA[2]  $lcBA[3]  $lcBA[4] $lcBA[5]\n";
    print OUT_LC "$lcBAl[1] $lcBAm[1] $lcBAs[1]\n";  #change late
    print OUT_LC "$lcBAl[2] $lcBAm[2] $lcBAs[2]\n";
    print OUT_LC "$lcBAl[3] $lcBAm[3] $lcBAs[3]\n";
    print OUT_LC "$lcBAl[4] $lcBAm[4] $lcBAs[4]\n";
    print OUT_LC "$lcBAl[5] $lcBAm[5] $lcBAs[5]\n";

    close(OUT);    
    close(OUT_LC);
}





###############################################################################
# Generate ascii data of emissions for other users
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################


sub hourlyascii{ 
    $outfilehalfhPM25BA=join('',"biomass_burning_",$yyyymmddhh);
    &rmduplicated;  ##remove duplicated locations  ###3/2/2009
    $dayf=$curyday;
    if($hour==0){
	$sth=18;
	$dayf=$preyday;
    }
    if($hour==6){
	$sth=0;
    }
    if($hour==12){
	$sth=6;
    }
    if($hour==18){
	$sth=12;
    }

    @allsp=NULL;
    $i5=0;

    foreach $line(@totaltiles)
    {
	chomp($line);
	@data=split(' ', $line);
           ####hourly data--MODIS fuel and AVHRR VCI
	$sum6=0;

	if(($data[54]>0)&&($data[1]>=$Lamin))  ###pm2.5 emission --0 degree northwards for netcdf file --10/2011
	{
	    $lineba= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $linepm25= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $lineco= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $linech4= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $lineco2= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $linenmhc= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $linenh3= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $linen2o= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $linenox= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $lineso2= sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 

#	    $allspecies=sprintf("%d %d %.2f %.2f ", $data[2],$dayf, $data[0], $data[1]);  #year,day, Lon Lan 
	    $allspecies=sprintf("%d %d %.2f %.2f %d ", $data[2],$dayf, $data[0], $data[1], $data[53]);  #year,day, Lon Lan 
	    for($i=18;$i<24;$i++)  #last 6 hours
	    {
		$hburn=0;
		$hpm25=0;
		$hco=0;
		$hch4=0;
		$hco2=0;
		$htnmhc=0;
		$hnh3=0;
		$hn2o=0;
		$hnox=0;
		$hso2=0;
		$k1=2*$i+4;
		$k2=$k1+1;
		if($data[$k1]>0) {$hburn=$hburn+$data[$k1];}
		if($data[$k2]>0) {$hburn=$hburn+$data[$k2];}
		if($hburn>0){
##moisture controlled
		    $hpm25=($hburn/$data[52])*$data[54];
		    $hco=($hburn/$data[52])*$data[58];
		    $hch4=($hburn/$data[52])*$data[60];
		    $hco2=($hburn/$data[52])*$data[62];
		    $htnmhc=($hburn/$data[52])*$data[64];
		    $hnh3=($hburn/$data[52])*$data[66];
		    $hn2o=($hburn/$data[52])*$data[68];
		    $hnox=($hburn/$data[52])*$data[70];
		    $hso2=($hburn/$data[52])*$data[72];
		}
		$sum6=$sum6+$hburn;
		$lineba.=sprintf("%.4f ",$hburn); 

		$linepm25.=sprintf("%.4f ",$hpm25);
		$lineco.=sprintf("%.4f ",$hco);
		$linech4.=sprintf("%.4f ",$hch4);
		$lineco2.=sprintf("%.4f ",$hco2);
		$linenmhc.=sprintf("%.4f ",$htnmhc);
		$linenh3.=sprintf("%.4f ",$hnh3);
		$linen2o.=sprintf("%.4f ",$hn2o);
		$linenox.=sprintf("%.4f ",$hnox);
		$lineso2.=sprintf("%.4f ",$hso2);
	    }
	 
	    if($sum6>0){		
		@xx=split(' ',$lineba);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$linepm25);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$lineco);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$linech4);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$lineco2);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$linenmhc);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$linenh3);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$linen2o);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$linenox);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);
		@xx=split(' ',$lineso2);
		$allspecies.=sprintf("%.4f %.4f %.4f %.4f %.4f %.4f ",$xx[4],$xx[5],$xx[6],$xx[7],$xx[8],$xx[9]);

		$fuel=0;
		$fuel=($data[74]+$data[75]+$data[77]+$data[78]+ $data[79]+$data[80]);
		$fuel=10*$fuel; #### Mg/km2, or g/m2**, total fuel
		$allspecies.=sprintf("%.4f ",$fuel); 

		$allspecies.=sprintf("%.4f ",$data[57]); 

		$allsp[$i5]=$allspecies;
		$i5++;	    
	    }
	}
    }


##generate PM2.5 and burned area--check late
    open(OUTall,">$ENV{WORK}/$outfilehalfhPM25BA");

    $hourn=$prehour-1;
    if($hourn<0){$hourn=23;}

    print OUTall "TotalFirePoints BeginingYear BeginingMonth BegingDay BegingTime EndingYear EndingMonth EndingDay EndingTime\n";
    if($i5>90000){
	&time_check("too many fires not to be able to generate NetCDF4 file. see biomass_burning_creatNetCdf4.c")
	}
    if($i5<1){
  &time_check("too few fires, preocessing may not correct")
    }

    if($prehour==0){
	print OUTall "$i5 $preyear $premon $premday $sth $preyear $premon $premday $hourn\n";
    }else{
	print OUTall "$i5 $curyear $curmon $mday $sth $curyear $curmon $mday $hourn\n";
    }


##    print OUTall "Year day longitude latitude ";
    print OUTall "Year day longitude latitude ecosystem ";

    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "BA_h$i ";
    }
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "PM25_h$i ";
    }
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "CO_h$i ";
    }
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "CH4_h$i ";
    }
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "CO2_h$i ";
    }
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "TNMHC_h$i ";
    }
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "NH3_h$i ";
    } 
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "N2O_h$i ";
    }
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "NOX_h$i ";
    }
    for($i=$sth;$i<$sth+6;$i++){
	print OUTall "SO2_h$i ";
    } 

    print OUTall "Fuel_loading Pixelsize\n";

    foreach $allspecies(@allsp){
	chomp($allspecies);
	print OUTall "$allspecies\n";
    }
    
    close OUTall;

    &time_check('reformat hourly data successfully');    #Prints time and status to log file  
}



##############################################################################
# Error and exception handling
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub checks{
    #checks to ensure against broken pipes, Camel book, 2nd ed., p338,341
    #if broken pipe then die
    $SIG{PIPE} = sub {die "Broken Pipe";};    
    #Writes message to log before exiting when die occurs
    #Currently writes time, error message, child process and system errors, 
    #and line 
    $SIG{__DIE__} = sub{
	$status = "Did NOT Finish";
	$msg = "$_[0]. Child Error = $? . System Error = $! .";
	local ($sec,$min,$hour) = (localtime(time))[0..2];
	open(LOG, ">>$outfiles{batchlog}");
	select(LOG);
	$~=ERROR;                #Formats at end of script
	write LOG;
	close LOG;
	select(STDOUT);
	# dies after exiting subroutine
    };
    #Writes message to log file when warning occurs.
    #Includes time, error message, line#, child process error and system error

    $SIG{__WARN__} = sub {
	#turns off warnings for this section   
	local $^W = 0;
        #Does not report uninit. values warinings. 
	unless ($_[0] =~ m/uninitialized/){      
	    $status = "Warning";
	    local ($sec,$min,$hour) = (localtime(time))[0..2];
	    #gives warning message, child error, system error
	    local $msg = "$_[0] Child Error = $?. System Error = $!.";
	    #opens log file to append
	    open(LOG, ">>$outfiles{batchlog}");
	    #selects LOG file handle and ERROR format
	    select(LOG);
	    $~ = ERROR; #Formats at end of script
	    write LOG;
	    close LOG;
	    #Reselect STOUT as format for normal status updates
	    select(STDOUT);
	    print "Warning: Possible error.  See log file\n";
	    #Clears error variables variables.  Otherwise, these will hold old
	    #values unless new error changes their value
	    undef $?;
	    undef $!;
        }
    };
}#end sub checks


###########################################################################
# starts a log file.  Info in log file in format LOGSTART.  
# Currently includes time,date,user,inputfiles
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub mklog{ 
    # if old log exists, rename it log.old
    if (-e "$outfiles{batchlog}"){
	rename("$outfiles{batchlog}","$outfiles{batchlog}.old") or 
	    die "Could not rename existing $outfiles{batchlog}";
                              }
    #Get time
    local ($sec,$min,$hour,$mday,$mon,$year) = (localtime(time))[0..6];
    # Open and write to log file
    open(LOG, ">$outfiles{batchlog}") or die("Could not open log file $outfiles{batchlog} ");
    select(LOG);
    $~ = "LOGSTART";     #Formats at end of script
    write LOG;
    close LOG;
    select STDOUT;
}


###############################################################################
# Write end of log
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub endlog{
    #Writes to log file if script completes successfully
    local ($sec,$min,$hour) = (localtime(time))[0..2];
    open(LOG, ">>$outfiles{batchlog} ") or die;
    select(LOG);
    $~ = "LOGEND"; #Formats at end of script
    write LOG;
    close LOG;
    select(STDOUT);
}


###############################################################################
# Write time and status to log file
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub time_check{
    $task = $_[0];
    local ($sec,$min,$hour) = (localtime(time))[0..2];
    open(LOG, ">>$outfiles{batchlog}");
    select(LOG);
    $~=TIME;
    write LOG;
    close LOG;
    select(STDOUT);
}



###############################################################################
# generate a jpg file of PM2.5 spatial distribution
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub idljpgmap{

    $idlinput="emissiondatainputNA";
    system("cp $outfilelanlon $idlinput"); 

    if(-e "$ENV{SOURCE}/setColorTableForLines.pro"){$newerr=0;}
    else{&time_check("file does not exist:setColorTableForLines.pro");}
    if(-e "$ENV{SOURCE}/colorbar.pro"){$newerr=0;}
    else{&time_check("file does not exist:colorbar.pro");}
    if(-e "$ENV{SOURCE}/circle.pro"){$newerr=0;}
    else{&time_check("file does not exist:circle.pro");}
    if(-e "$ENV{SOURCE}/fire_image_NA.pro"){$newerr=0;}
    else{&time_check("file does not exist:fir_imagedis_ascii_NA.pro");}

    open(IDL,"|/usr/local/bin/idl") or $newerr=1;
        ##   open(IDL,"|/usr/local/bin/idl71") or $newerr=1;
    &time_check("could not open file: IDL package") if $newerr;
    $newerr=0;
    
    print IDL ".r $ENV{SOURCE}/setColorTableForLines.pro \n";
    print IDL ".r $ENV{SOURCE}/colorbar.pro \n";
    print IDL ".r $ENV{SOURCE}/circle.pro \n";
    print IDL ".r $ENV{SOURCE}/fire_image_NA.pro\n";
    print IDL "FIRE_IMAGE_NA\n";
    close(IDL);

    $idloutput=$idlinput;
    $idloutput.=".jpg";
    $jpgfileNA=$outfilelanlon;
    $jpgfileNA.="_NA.jpg";
    system("mv $idloutput $jpgfileNA");

    &time_check('complete to generate burned area JPG file');    #Prints time and status to log file  
}





###############################################################################
# Generate a netCDF file of burned area and emissions 
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub creatnetcdf4{

    $cdffile=$outfilehalfhPM25BA;
    $cdffile.=".nc";

    if(-e "$ENV{SOURCE}/biomass_burning_creatNetCdf4.exe") {$newerr=0;}
    else{  &time_check('biomass_burning_creatNetCdf4.exe does not exist'); }
    if(-e "$outfilehalfhPM25BA") {$newerr=0;}
    else{  &time_check("$outfilehalfhPM25BA"); }


    system("$ENV{SOURCE}/biomass_burning_creatNetCdf4.exe $outfilehalfhPM25BA $cdffile");
    &time_check('complet to generate netCDF file successfully');    #Prints time and status to log file  
}




###############################################################################
# Remove the double calculations caused by matching up of fires and fuel data  
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################

sub rmduplicated
{
  ##remove duplicated locations   ####11/26/2008
    @outascii2=NULL;
    $i=0;
  ## $outascii=fYYYYDDD.out_ascii;
  ##  open (TILEID, "<./$outascii") || die "couldn't open file of tile index";
    open (TILEID, "<$ENV{WORK}/$outascii") or $newerr=1; 
    @totaltiles=<TILEID>;
    close TILEID;
    &time_check("could not open file: $outascii") if $newerr;
    $newerr=0;

    foreach $line (@totaltiles){
	chomp($line);
	@da2=split(' ',$line);
	$lola=join('',$da2[0],$da2[1]);
	$outascii2[$i]=join(';',$lola, $line); 
	$i++;
    }

    @totalem2 = sort {(split ';', $a)[0] cmp (split ';', $b)[0]}@outascii2;
    $old=0;
    $i=0;
    @totaltiles=NULL;
    foreach $line (@totalem2){
	chomp($line);
	@da2=split(';',$line);
	if($da2[0] eq $old){
	    $lola=0;
	}else{
	    $old=$da2[0];    ###jan--2009
	    $totaltiles[$i]=$da2[1];
	    $i++;
	}
    }
}



###############################################################################
# Generate statistical information of burned area in a jpg file  
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################


sub QA_BurnedArea{

 ##(1)spatial pattern of burned area within 6h for 0.25d 
 ##(2) burned area in different ecosystem with 6 hour

    $idlinput="lc_temp";

    if(-e "$ENV{SOURCE}/plot_bar_chart.pro"){$newerr=0;}
    else{&time_check("file does not exist:plot_bar_chart.pro");}

 ###   $idlinput="lc_tempX";
   #    open(IDL,"|/usr/local/bin/idl71") or $newerr=1;  
    open(IDL,"|/usr/local/bin/idl") or $newerr=1;  
   ###    print IDL ".compile fir_imagedis_ascii.pro\n";";
    &time_check("could not open file: IDL package") if $newerr;
    $newerr=0;

    print IDL ".r $ENV{SOURCE}/plot_bar_chart.pro\n";
    print IDL "PLOT_BAR_CHART\n";
    close(IDL);

    $idloutput=$idlinput;
    $idloutput.=".jpg";
    $jpgfileqa=$outfilelanlon;
    $jpgfileqa.="_QA.jpg";
    system("mv $idloutput $jpgfileqa");
 }

###############################################################################
# In diurnal fitting, the geolocatio in MODIS and AVHRR fires is not changed
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################


sub MODISAVHRRpix{

    $lastline="day_lanlog, year, day, hour, min, GOESlon, GOESlan, Satzen, pixelsize, FireSize, Temp, Eco, Flag, longitude, latitude, dlon, dlan"; 

    push(@pixel, $lastline);   ##add a line to allow to process last line

    $j = scalar (@pixel);
    @pixelma=NULL;
    $k1=0;
    $pixelma[$k1]=$pixel[0];
    $k1++; 

    @datap=split(', ',$pixel[0]);
    $oldday=$datap[13];  ##original lon/lan
    $oldday.=$datap[14];
   


    for($j1=1;$j1<$j;$j1++)
    {
	@datap=split(', ',$pixel[$j1]);
	
	$modislonlan=$datap[13];  ##original lon/lan
	$modislonlan.=$datap[14];
  
  
###	if($datap[0] eq $oldday)  #get fir in the same location of MOIDIS
	if($modislonlan eq $oldday)
	{
	    $pixelma[$k1]=$pixel[$j1];
	    $k1++;
	}else{ ##print

	    for($k2=0;$k2<48;$k2++)
	    {
		$firesize[$k2]=0;  #intial values
		$adfiresize[$k2]=-100;
	    }
	    $summod=0;
	    for($k2=0;$k2<$k1;$k2++){
		@datap=split(', ',$pixelma[$k2]);
		if(($datap[4]>=0)&&($datap[4]<30))   
		{
		    $h=2*$datap[3];
		}
		if(($datap[4]>=30)&&($datap[4]<60))  
		{
		    $h=2*$datap[3]+1;
		}

		$origlc=$datap[11];

		$firesize[$h]=$firesize[$h]+$datap[9];   ###several MODIS/AVHRR fire pixels may exist in a GOES footprint 

       		$summod=$summod+$datap[9];
      		$MaxPixel=$datap[8];
	    }

	    if($summod>0.5*$MaxPixel){$summod=0.5*$MaxPixel;}  #######2012

	    $start1=-9;  ##fire begining
	    $end1=-9;   ##fire ending
	    $duration=0;

	    for($k2=0;$k2<48;$k2++) 
	    {
		if(($firesize[$k2]>0)&&($start1<0))
		{
		    $start1=$k2;
		}
	    }
	    for($k2=47;$k2>=0;$k2--) 
	    {
		if(($firesize[$k2]>0)&&($end1<0))
		{
		    $end1=$k2;
		}
	    }

	    $end1=$end1+2;   ## 1 hour
	    $start1=$start1-2;  ##1 hour
	    if($end1>47) {$end1=47;}
	    if($start1<0) {$start1=0;}

	     for($k2=$start1;$k2<=$end1;$k2++){  #allocate the values equally.    
		$adfiresize[$k2]=$summod/($end1-$start1+1); 
	     }    
	
###	    $outd= join(' ',$datap[5], $datap[6], $datap[1], $datap[2]);
	    $outd= join(' ',$datap[13], $datap[14], $datap[1], $datap[2]);   ###MODIS location

	    for($k1=0;$k1<48;$k1++)  #addressed fire size
	    {
		$outd.=sprintf(" %.4f", $adfiresize[$k1]);
	    }

	    $modisavhrr=1;   ###modis/avhrr pixel
	    if(($summod>0)&&($summod<=0.65*$MaxPixel)){  #2012
		print OUT "$outd $summod $origlc $MaxPixel $modisavhrr\n";  ##ecosystem with USGS LCCS pixelSize 7/2011
		}

	}
	
	@pixelma=NULL;
	$k1=0;
	$pixelma[$k1]=$pixel[$j1];

	$pixelma[$k1]=$pixel[$j1];
	$k1++; 

	@datap=split(', ',$pixel[$j1]);
	$oldday=$datap[13];  ##original lon/lan
	$oldday.=$datap[14];
   

##	    print OUT "$outd $subtal $origlc $MaxPixel\n";  ##ecosystem with USGS LCCS pixelSize 7/2011

    }
}


###############################################################################
# Allocate burned area to grid cells
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov              01/10/2012
#
###############################################################################


sub grid{

    $grid_burn1km="temp_1km";
    $grid_burn12km="temp_12km";

    if(-e $grid_burn1km) {unlink($grid_burn1km)};
    if(-e $grid_burn12km) {unlink($grid_burn12km)};

    if(-e "$ENV{SOURCE}/goes_hms1km2_md.exe") {$newerr=0;} #modified by Pack_BA
    else{  &time_check('goes_hms1km2_md.exe does not exist'); } #modified by PackBA
    if(-e "$GoesHmsFited") {$newerr=0;}
    else{  &time_check("$GoesHmsFited does not exist"); }
    if(-e "$GoesHmsCluster") {$newerr=0;}
    else{  &time_check("$GoesHmsCluster does not exist"); }
 
if(-e "$grid_burn1km_raw"){  #added by Pack_BA
unlink $grid_burn1km_raw;  #added by Pack_BA
}

#    system("$ENV{SOURCE}/goes_hms1km.exe $GoesHmsFited $GoesHmsCluster $grid_burn1km $grid_burn12km"); 
system("$ENV{SOURCE}/goes_hms1km2_md.exe $GoesHmsFited $GoesHmsCluster $grid_burn1km_raw $grid_burn12km"); #replaced by Pack_BA
    &time_check('complet to generate grid cell data successfully');    #Prints time and status to log file  

#--following added by Pack_BA
### print("x=$grid_burn1km_raw, $Temp_BACum, $Temp_BAFlag\n");

  if(-e "$Temp_BACum") {$newerr=0;}
    else{  &time_check("$Temp_BACum does not exist"); }
  if(-e "$Temp_BAFlag") {$newerr=0;}
    else{  &time_check("$Temp_BAFlag does not exist"); }
 
  system("$ENV{SOURCE}/BArelocation.exe $Temp_BAFlag $Temp_BACum $grid_burn1km_raw $grid_burn1km");
  if(-e "$grid_burn1km") {
   &time_check("complet to generate $grid_burn1km successfully");    #Prints time and status to log file
 }else{
   &time_check("can not generate $grid_burn1km");    #Prints time and status to log file
 }

}


###############################################################################
# Create a GRIB2 file for sum of six-hour burned area with coverage
# -170W--45W, 80N-0N, resolution 1/8
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################


sub create_grib2{
  ###10/2011
  ##1km domain

    ## $modisdata="hms2011111806.prelim.txt";
     # $year=2011;
    # $month=11;
    # $day=18;
     # $hour=6;
   #######$yyyymmddhh==20111102_06_11

    $minute=0;  
    if($yyyymmddhh=~m!([0-9]{4})([0-9]{2})([0-9]{2})_([0-9]{2})!){  ####10/2011
	$year=$1;
	$month=$2;                           
	$day=$3;
	$hour=$4;
    }

 #  print "yyyymmddhh==$yyyymmddhh, $year,$month,$day,$hour\n";

    if(-e "$ENV{STINPUT}/LWmask_lambert_CON1km_NA"){$newerr=0;}
    else{&time_check("file does not exist:LWmask_lambert_CON1km_NA");}

    if(-e "$ENV{STINPUT}/LWmask_lambert_CON12km"){$newerr=0;}
    else{&time_check("file does not exist:LWmask_lambert_CON12km");}

    if(-e "$ENV{SOURCE}/grib2_burnedarea_lamCON.exe"){$newerr=0;}
    else{&time_check("file does not exist:grib2_burnedarea_lamCON.exe");}


      ###twice a day for 1km burned area
     ##if(($hour==0)||($hour==12))
    if(($hour==6)||($hour==18))
    {
	$paramet="temp_pa";  
	open(OUTpa,">$ENV{WORK}/$paramet");

	$grid_size=1;   ##km2
	$temph=$hour+5;  ##ending of the time period
	print OUTpa "$grid_size\n";
	print OUTpa "$year\n";
	print OUTpa "$month\n";
	print OUTpa "$day\n";
          #   print OUTpa "$hour\n";
	print OUTpa "$temph\n";
	print OUTpa "$minute\n";
	close(OUTpa);
          ##   $grib2_burn=$outfilelanlon;
        #    $hour2=$hour+11;
        #    $grib2_burn1km=join('',"burned_area_",$year,$month,$day,"_",$hour,"_",$hour2);
	if($hour==6){
	    $grib2_burn1km=join('',"burned_area_",$year,$month,$day,"_00_11");
	}
	if($hour==18){
	    $grib2_burn1km=join('',"burned_area_",$year,$month,$day,"_12_23");
	}

	$grib2_burn1km.="_1km.grib2";
   ## $lwfile="/net/orbit268l/home/xiaoyang/XYZ/GRIB/USR_xyz/LC/LWmask_lambert_CON1km_NA";
	$lwfile="$ENV{STINPUT}/LWmask_lambert_CON1km_NA"; 
	system("$ENV{SOURCE}/grib2_burnedarea_lamCON.exe $grid_burn1km $lwfile $paramet $grib2_burn1km");  
	&time_check('complet to generate 1km grib2  successfully');    #Prints time and status to log file
    }


    ##12km domain
    $paramet="temp_pa";
    open(OUTpa,">$ENV{WORK}/$paramet");

    $grid_size=12;   ##km2
    $temph=$hour+5;  ##beginning of the time period
    print OUTpa "$grid_size\n";
    print OUTpa "$year\n";
    print OUTpa "$month\n";
    print OUTpa "$day\n";
##    print OUTpa "$hour\n";
    print OUTpa "$temph\n";
    print OUTpa "$minute\n";
    close(OUTpa);
    $grib2_burn12km=$outfilelanlon;
    $grib2_burn12km.="_12km.grib2";

    $lwfile="$ENV{STINPUT}/LWmask_lambert_CON12km"; 
    system("$ENV{SOURCE}/grib2_burnedarea_lamCON.exe $grid_burn12km $lwfile $paramet $grib2_burn12km"); 

          # print "************333yyyymmddhh==$yyyymmddhh, $year,$month,$day,$hour\n";
 
    &time_check('complet to generate 12km grib2  successfully');    #Prints time and status to log file

}






###########################################################
#   Formats
#   These format the output to the log file.  Called in the various
#   log functions (i.e., endlog) and by warn()
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
###############################################################################


format LOGSTART =

        @<<<<<<<<<< run at @>>:@>>:@>> on @>>/@>>/@>> by @<<<<<<<<<<<<<<<<<<<
         $0,                 $hour, $min, $sec, $mon, $mday, $year, $ENV{USER}
.

#Time

format TIME = 

        @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $task

        Time is @>>:@>>:@>>
                 $hour, $min, $sec

.

#Successful completion of scripts

format LOGEND = 

        Finished @>>:@>>:@>>
                 $hour, $min, $sec
.

#Error running scripts

format ERROR = 
         Error at        @<<:@<<:@<<
                         $hour,$min,$sec
         @<<<<<<<<<<<<<<<: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
         $status          ,$msg
                         ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                          $msg
                         ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                         $msg
                         ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                         $msg
                         ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                         $msg
.
                         

##########################################################################
#   Check processing error and send an email
#   
# 
#
#
#        Developers: Xiaoyang Zhang
#                    ERT at NOAA/NESDIS/STAR
#                    xiaoyang.zhang@noaa.gov
#
############################################################################

sub file_check_email{

    $logfile="log";
    open (TILEID, "<$ENV{WORK}/$logfile") or $newerr=1; 
    @totalline=<TILEID>;
    close TILEID;

    $incorrect=0;
    foreach $line(@totalline){
	chomp($line);

	if($line=~m/not/){
	    $incorrect=1;
	}	
    }

    if($incorrect==1){
	$title='Perl Mail demo';
	$to='gilberto.vicente@noaa.gov';
	$from= 'webmaster@YOURDOMAIN.COM';
	$subject='Error in calculating burned area and emission';


 
	open(MAIL, "|/usr/sbin/sendmail -t");
	
## Mail Header
	print MAIL "To: $to\n";
	print MAIL "From: $from\n";
	print MAIL "Subject: $subject\n\n";
## Mail Body
	print MAIL "There may be errors in $yyyymmddhh. Please check log file\n";
 
	close(MAIL);
    }

}



#################################################################
#
# writen by G. Vicente, April/2012 
# Purpose:     put the latest BBEP-geo product files in the SATEPSANON web server
#
########################################################################################
use Net::FTP;
use Net::Netrc;
use Date::Calc qw(:all);

#$localDir='/home/oper/BBEP2/OUTPUT';
#$localDirSAVE='/home/oper/BBEP2/OUTPUT/SAVE_OLD';
$localDir='/home/Pbbep/BBEP2/OUTPUT';
$localDirSAVE='/home/Pbbep/BBEP2/OUTPUT/SAVE_OLD';

$remoteDir='/data/ftp/pub/FIRE/BBEP-geo/tmp';

open(SFTP,'|sftp oper@satepsanon.nesdis.noaa.gov');
print SFTP "cd $remoteDir\n";
print SFTP "lcd $localDir\n";
print SFTP "mput *\n";

print SFTP "quit\n";
close(SFTP);

print "finished putting file on satepsanon\n";
print "done\n";

#Transfer old files from OUTPUT to OUTPUT/SAVE_OLD

chdir $localDir;
    system("mv *.*  $localDirSAVE/");

#--Clean the ABBA_DIR directory
#$localABBAdir='/home/oper/BBEP2/ABBA_DIR';
#    system("rm $localABBAdir/*");

#--Clean VCI_DIR
#$localVCIdir='/home/oper/BBEP2/VCI_DIR';
#    system("rm $localVCIdir/*");

#--Clean the HMS *.txt files from the HMS dir
#$localHMSdir='/home/oper/BBEP2/HMS';
#    system("rm $localHMSdir/*");
