#!/usr/bin/perl -I/nwprod/lib/incmod/perl

# $Id$

#------------------------------------------------------------------------------
# Filename:  sub_write_burns.pl 
#    
# Purpose:  Sum the emissions from a burn for the burn report.  Sum the
# emissions on a daily basis for most fire types except for EPAPRESCRIBED
# fires which are summed on an hourly basis.  EPAPRESCRIBED not yet
# implemented.

# USDA Forest Service - Pacific Northwest Wildland Fire Labratory
# Copyright (C) 2003-2004
#------------------------------------------------------------------------------

use Text::CSV_XS; 
use IO::File;

#GSM change paths; the FRAMEWORK directory should be ush
$DIR_HOME = exists $ENV{BSKY_HOME} ? $ENV{BSKY_HOME} : '/bluesky';

$cwd = `pwd`;
# $cwd = system(pwd);
print " in smoke_sub_write_burns.pl the cwd is $cwd \n";

require "$USHsmoke/smoke_sub_global.pl";

sub write_burn_output
{

    chdir("$MODELS_BURN_DIR");
    $cwd = `pwd`;
    # $cwd = system(pwd);
    print " in smoke_sub_write_burns.pl the cwd is $cwd \n";

 
    my $self = shift;
    my (@time, @heatrelease, @pm25, @pm10, @pm, @co, @co2, @ch4, @nmhc);
    my $i = 0;
 
    my @burn_output       = qw(time heat        pm25 pm10 pm co co2 ch4 nmhc);
#   my @burn_output_units = qw(min  btu ton_per_day ton_per_day ton_per_day ton_per_day ton_per_day ton_per_day ton_per_day );
    my @burn_output_units = qw(min  btu    kg/hr       kg/hr       kg/hr       kg/hr       kg/hr       kg/hr       kg/hr    );
 
    my @burn_info = qw(ID LAT LON STATE AREA DATE TYPE VEG 1HR 10HR 100HR 1kHR 10kHR 10k+HR);

print " in smoke_sub_write_burns.pl the BSKY_NUMFIRES is $BSKY_NUMFIRES \n";

    for ( my $j = 0; $j < $BSKY_NUMFIRES; $j++ ) {

print " in smoke_sub_write_burns.pl the for-loop j is $j \n";

        my $bref = $BURN[$j];

        debug ( 100, "writing fire{ID} = $$bref{ID}, fire{TYPE} = $$bref{TYPE}");

        if ( $$bref{TYPE} eq 'EPAPRESCRIBED' ) {
            $output_interval = 60;    # minutes
        }
        else {
            $output_interval = 1440;  # minutes
        }
    
#       my $epm_in_file = $MODELS_EMISSIONS_DIR."/".$$bref{ID}.".EPM";
#       my $epm_out_file = $MODELS_BURN_DIR."/".$$bref{ID}.".OUT";
        my $epm_in_file = $DIR_HOME."/working/".$$bref{ID}.".EPM";
        my $epm_out_file = $DIR_HOME."/working/".$$bref{ID}.".OUT";
        debug ( 200, "opening file $epm_in_file");
        debug ( 200, "opening file $epm_out_file");
        open(EPM_IN_FILE, "< $epm_in_file");
        open(EPM_OUT_FILE, "> $epm_out_file");

        # read and ignore header
        for (1...6) { <EPM_IN_FILE>; }

        print EPM_OUT_FILE "Fire ID        = $$bref{ID}\n"; 
        print EPM_OUT_FILE "Latitude       = $$bref{LAT}\n"; 
        print EPM_OUT_FILE "Longitude      = $$bref{LON}\n"; 
        print EPM_OUT_FILE "State          = $$bref{STATE}\n"; 
        print EPM_OUT_FILE "Area (acres)   = $$bref{AREA}\n"; 
        print EPM_OUT_FILE "Date           = $$bref{DATE}\n"; 
        print EPM_OUT_FILE "Type           = $$bref{TYPE}\n"; 
        print EPM_OUT_FILE "Vegetation     = $$bref{VEG}\n"; 
        print EPM_OUT_FILE "1-hr Fuel      = $$bref{'1HR'}\n"; 
        print EPM_OUT_FILE "10-hr Fuel     = $$bref{'10HR'}\n"; 
        print EPM_OUT_FILE "100-hr Fuel    = $$bref{'100HR'}\n"; 
        print EPM_OUT_FILE "1000-hr Fuel   = $$bref{'1kHR'}\n"; 
        print EPM_OUT_FILE "10000-hr Fuel  = $$bref{'10kHR'}\n"; 
        print EPM_OUT_FILE "10000+-hr Fuel = $$bref{'10k+HR'}\n"; 

        foreach $col (@burn_output) {
            print EPM_OUT_FILE "$col, "
        }
        print EPM_OUT_FILE "\n";

        foreach $col (@burn_output_units) {
            print EPM_OUT_FILE "$col, "
        }
        print EPM_OUT_FILE "\n";
    
        # read data into arrays and write to burns.csv at every OUTPUT_INTERVAL
        # array_stat[0] is total
        # array_stat[1] is min
        # array_stat[2] is max
        # array_stat[3] is avg

        while ($_ = <EPM_IN_FILE>) {

            ($time[$i], $heatrelease[$i], $pm25[$i], $pm10[$i], $pm[$i], $co[$i],
	     $co2[$i], $ch4[$i], $nmhc[$i]) = split;

            if ( ($time[$i] % $output_interval) == 0 ) {

                # modulus of $output_interval reached

                my $dt = $time[1] - $time[0];
                my $convert_to_sec = ($time[$i]-$time[0] + $dt)*60 ;  # delta t (in min) * sec/min
                my $convert_to_tons = $convert_to_sec*0.000001 ;  # tons/g

                my $heatrelease_out = (array_stat(@heatrelease))[3]*$convert_to_sec;
                my $pm25_out        = (array_stat(@pm25))[0]*$convert_to_tons;
                my $pm10_out        = (array_stat(@pm10))[0]*$convert_to_tons;
                my $pm_out          = (array_stat(@pm))[0]*$convert_to_tons;
                my $co_out          = (array_stat(@co))[0]*$convert_to_tons;
                my $co2_out         = (array_stat(@co2))[0]*$convert_to_tons;
                my $ch4_out         = (array_stat(@ch4))[0]*$convert_to_tons;	 
                my $nmhc_out        = (array_stat(@nmhc))[0]*$convert_to_tons;	 

                # write output
#               print EPM_OUT_FILE "$time[$i], $heatrelease_out, $pm25_out, $pm10_out, $pm_out, $co_out, $co2_out, $ch4_out, $nmhc_out \n";

                # zero arrays
                $i = -1;
                @time = (); @heatrelease = (); @pm25 = (); @pm10 = (); @pm = (); @co = ();
                @co2 = (); @ch4 = (); @nmhc = ();
            }
            $i++;
        }
        if ( $#time > 0 ) {

            # write remaining output

            my $dt = $time[1] - $time[0];
            my $convert_to_sec = ($time[$i-1]-$time[0] + $dt)*60 ;  # delta t (in min) * sec/min
            my $convert_to_tons = $convert_to_sec*0.000001 ;  # tons/g

            my $convert_to_mass = ($dt)*60 ;  # delta t (in min) * sec/min
            my $convert_to_rate = $convert_to_mass*0.001/(($time[$i-1]-$time[0])/60) ;  # kg/g  / (min * hr/min)

            my $heatrelease_out = (array_stat(@heatrelease))[3]*$convert_to_sec;
            my $pm25_out        = (array_stat(@pm25))[0]*$convert_to_rate;
            my $pm10_out        = (array_stat(@pm10))[0]*$convert_to_rate;
            my $pm_out          = (array_stat(@pm))[0]*$convert_to_rate;
            my $co_out          = (array_stat(@co))[0]*$convert_to_rate;
            my $co2_out         = (array_stat(@co2))[0]*$convert_to_rate;
            my $ch4_out         = (array_stat(@ch4))[0]*$convert_to_rate;	 
            my $nmhc_out        = (array_stat(@nmhc))[0]*$convert_to_rate;	 

            # write output
            print EPM_OUT_FILE "$time[$i-1], $heatrelease_out, $pm25_out, $pm10_out, $pm_out, $co_out, $co2_out, $ch4_out, $nmhc_out \n";

            # zero arrays
            $i = 0;
            @time = (); @heatrelease = (); @pm25 = (); @pm10 = (); @pm = (); @co = ();
            @co2 = (); @ch4 = (); @nmhc = ();
        }
    }
    close (EPM_IN_FILE);
    close (EPM_OUT_FILE);
};

return 1;
