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

# $Id$

#------------------------------------------------------------------------------
# Filename:  smoke_sub_burn_system_EPAWILDFIRE.pl
#
# Purpose: Package implementing interface with the manual creation of the
# wildfire info file.
#
# By:  smo 06/21/2004
#
#  USDA Forest Service - Pacific Northwest Wildland Fire Labratory
#  Copyright (C) 2003-2004
#------------------------------------------------------------------------------

package EPAWILDFIRE;

print (" Luke- in smoke_sub_burn_system_EPAWILDFIRE.pl \n");

$BURN_FILE = exists $ENV{FDATE} ? $ENV{FDATE} : '2001010100';
$DIR_HOME = exists $ENV{BSKY_HOME} ? $ENV{BSKY_HOME} : '/bluesky';

$USHsmoke = $ENV{USHsmoke};
require "$USHsmoke/smoke_sub_burn_system_baseclass.pl";
print("luke- in WILDFIRE checking BURN_FILE = $BURN_FILE");

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

@ISA = (Burn_System_Class);

sub new
{
    ::debug(1, "using manually created Wildfire reports burn system");
    my $self = Burn_System_Class->new;

    $self{OUTPUT_INTERVAL} = 1440;

    @::EPAWILDFIRE_FILE_NAMES = ("$BURN_FILE.csv");

    bless($self);
    return $self;
};

sub get_load_burn_data
{
    my @columns = qw(ID LAT LON TWN STWNSub TWND RNG SRNG RNGD SECT
                  ELEV OWN STATE CNTY AREA DATE TIME
                  DUR TYPE PM25 PM10 PM CO CO2 CH4 NMHC
                  HARV SNOW VEG 1HR 10HR 100HR 1kHR 10kHR 10k+HR
                  SHRUB GRASS ROT DUFF LITTER SLOPE FMM FM10 FM1k RAIN WIND);

    #------------------------------------------------------------------------------
    # Setup the two arrays that determine how a wildfire is processed thru a day.
    # Number of entries in these two arrays must be the same.
    #------------------------------------------------------------------------------
    @fireStartTimes_local_HHMM = ("1200"); # HHMM
    @percentAllocate           = (1.0);   # decimal %
 
    $cwd = `pwd`;
    print ("luke- inside get_load_burn_data the cwd is $cwd \n");
    for ( my $i = 0; $i <= $#::EPAWILDFIRE_FILE_NAMES; $i++ ) {

      $cwd = `pwd`;
      print ("luke-$i inside of for loop get_load_burn_data the cwd is $cwd \n");

        $infile  = @::EPAWILDFIRE_FILE_NAMES[$i];  # HARDCODED filename
        print("luke- in WILDFIRE checking infile = $infile");

        if ( -e "$infile" ) {

            print "processing burn file: $infile\n";

            my $yyyymmdd  = substr($infile, 0, 8);

            # open the input file as read only
            my $ifile_handle = new IO::File "< $infile";
            die "FATAL ERROR: $! unable to open file $infile\n" unless defined $ifile_handle;

            # use Text::CSV_XS parser
            my $csv = Text::CSV_XS->new();

            # get the input file header (1st line of file)
            my $header = $csv->getline($ifile_handle);

            # loop over the fires
            my $line;
            until($ifile_handle->eof()) {

                $line = $csv->getline($ifile_handle);

                my %fire;
                @fire{@$header} = @$line;

                print "Loading data for fire $fire{ID} from file $infile\n";

                # clean leading/trailing spaces
                while ( ($key, $value) = each(%fire) ) {
                    $fire{$key} =~ s/^\s+//;
                    $fire{$key} =~ s/\s+$//;
                }

                # in fire ID remove spaces, substitute _ for &, make upper case, append yyyymmdd
                $fire{ID} =~ s/\s+//g;
                $fire{ID} =~ s/&/_/g;
                $fire{ID} = uc $fire{ID};
                $fire{ID} = "$fire{ID}_$yyyymmdd";

                my %current_fire = (%fire);

                $current_fire{DATE} = $yyyymmdd;
                $current_fire{TYPE} = "EPAWILDFIRE";

                $::BURN[$::BSKY_NUMFIRES] = \%current_fire;
                ::debug(100, "loading data for burn number $::BURN[$::BSKY_NUMFIRES]{ID}");
                $::BSKY_NUMFIRES++;
            }
            $ifile_handle->close();
            ::debug(100, "after WILDFIRE load bsky_numfires = $::BSKY_NUMFIRES");
        }
        else {
            print "unable to obtain $infile\n ";
        }
    }
};

return 1;
