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

# $Id: sub_emissions_model_EPM.pl,v 1.2 2004/02/17 22:53:27 smo Exp $

#------------------------------------------------------------------------------
# Filename:  sub_emissions_model_EPM.pl
#
# Purpose:  Package implementing interface with the EPM/CONSUME v1.02 model.
#
# By:  smo
#
#  USDA Forest Service - Pacific Northwest Wildland Fire Labratory
#  Copyright (C) 2003-2004
#------------------------------------------------------------------------------

package EPM;

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

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

$USHsmoke = $ENV{USHsmoke};
$EXECsmoke = $ENV{EXECsmoke};

require "$USHsmoke/smoke_sub_emissions_model_baseclass.pl";

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

@ISA = (Emissions_Model_Class);

sub new
{
    ::debug(1, "using EPM emissions");
    my $self = Emissions_Model_Class->new;
    bless($self);
    return $self;
};

sub run_model
{
    shift;  # have to get rid of class call!
    my $fire = shift; # now can get hash ref to fire
    my ($j) = (shift, shift);
    ::debug(100, "running EPM model on $$fire{ID}");
    write_inputs(\%{$fire});
#    ::run_command("running epm", "$FRAMEWORK_DIR/models/emissions/EPM/epm");
    ::run_command("running epm", "$EXECsmoke/hysplit_epm");
#   parse_output(\%{$fire});
    ::run_command("moving epm input", "mv puffstuf.set $$fire{ID}.set");
    ::run_command("moving epm input", "mv puffstuf.run $$fire{ID}.run");
    ::run_command("moving epm output", "mv puffstuf.epm $$fire{ID}.EPM");
};


sub write_inputs
{
    # adapted from Trent's code by Sim

    # get reference to hash of data for this fire
    my $fire = shift;

    # run file
    # open file for writting
    open(OUT, "> puffstuf.run")
	or die "FATAL ERROR: $! Unable to open puffstuf.run";
    {
	printf(OUT " %9d = Burn date [YYYYMMDD]\n", $$fire{DATE});
	printf(OUT " %9d = Ignition time [HHMM] (UNUSED)\n", substr($$fire{TIME},0,4));
	printf(OUT " %9d = Number of ignition periods    [1 -  10]\n", 1);
	printf(OUT " %9d = Ignition Area for Period [acres]\n", $$fire{AREA});
	printf(OUT " %9d = Ignition interval for period [min]\n", $$fire{DUR});
	printf(OUT " %9d = Waiting interval after period 1 [minutes]\n", 30);
	printf(OUT " %9d = 1000-hr Fuel Moisture [%]\n", $$fire{FM1k});
	printf(OUT " %9s = Fuel Moisture Method\n", $$fire{FMM});
	printf(OUT " %9d = 10-hr fuel moisture  [%]\n", $$fire{FM10});
	printf(OUT " %9d = Days Since Last Rain\n", $$fire{RAIN});
	printf(OUT " %9d = Surface Wind Speed [mph]\n", $$fire{WIND});
	printf(OUT " %9d = Reporting interval [min] for EPM\n", 3);
	printf(OUT " %9d = End time [min] for reporting by EPM\n", 4320);
    }
    close(OUT);

    # set file
    # open file for writting
    open (OUT, "> puffstuf.set")
	or die "FATAL ERROR: $! Unable to open puffstuf.set";
    
    #----------------------------------------
    # Make sure the veg type is M, D, P, or H
    #----------------------------------------
    my $veg = substr($$fire{VEG},0,1);
    if ( $veg eq 'm' ) { $veg = 'M'; }  # Mixed Conifer
    if ( $veg eq 'd' ) { $veg = 'D'; }  # Douglas Fir
    if ( $veg eq 'p' ) { $veg = 'P'; }  # Ponderosa Pine
    if ( $veg eq 'h' ) { $veg = 'H'; }  # Hardwood

    if ( ($veg ne 'M') && ($veg ne 'D') && ($veg ne 'P') && ($veg ne 'H') ) {
       ::debug(100, "Warning:  veg = $veg, Need M, D, P or H for EPM, defaulting to P");
       $veg = 'P';
    }

    #----------------------------------------------------------
    # Make sure the owner is N, O, or P - set to N as default
    #----------------------------------------------------------
    my $own = $$fire{OWN};
    $own = 'N' if($own eq "Forest Service");
    $own = 'N' if($own eq "US Forest Service");
    $own = 'N' if($own eq "BLM");
    $own = 'N' if($own eq "Bureau of Indian Affairs");
    $own = 'P' if($own eq "Private");
    $own = 'N' if( ($own ne 'N') and ($own ne 'P') and ($own ne 'O') );
    ::debug(100, "own = $own");

    #---------------------------------------------------------------
    # Determine whether the fire is E(ast) or W(est of the Cascades.
    #---------------------------------------------------------------
    ::debug(100, "fire{LON} = $$fire{LON}");
    my $Region = 'E';
    if ( $$fire{LON} ge -121.0 ) {
       ::debug(100, "fire{LON} ge -121");
       $Region = 'W';
    }

    #---------------------------------------------------------------------------
    # Determine whether a fire should be classified as O(regon) or W(ashington).
    # EPM only recognizes O or W (not MT or ID).
    #---------------------------------------------------------------------------
    ::debug(100, "fire{STATE} = $$fire{STATE}");
    my $State = substr($$fire{STATE},0,1);
    if ( ($State ne 'W') and ($State ne 'O') ) {
       if ( $$fire{LAT} ge 45.8 ) {
          $State = 'W';
       }
       else {
          $State = 'O';
       }
    }
    {
        printf(OUT " %9s = Region\n", $Region . $State);
        printf(OUT " %9s = Ownership [N = National; P = Private; O = Other]\n", $own);
        printf(OUT " %9.2f = Degrees North Latitude\n", $$fire{LAT});
        printf(OUT " %9.2f = Degrees West Longitude (UNUSED)\n", $$fire{LON});
        printf(OUT " %9d = Area of Burn Site [acres] (UNUSED)\n", $$fire{AREA});
        printf(OUT " %9d = Harvest Date [yyyymm]\n", $$fire{HARV});
        printf(OUT " %7s%02d = Snow-Melt Month [mm]\n", '', $$fire{SNOW});
        printf(OUT " %9s = Vegetation Type [DPMH]\n", $veg);
        printf(OUT " %9.1f = 0.00 - 0.25 inch fuel [tons/acre]\n", $$fire{'1HR'});
        printf(OUT " %9.1f = 0.25 - 1.00 inch fuel [tons/acre]\n", $$fire{'10HR'});
        printf(OUT " %9.1f =    1 - 3    inch fuel [tons/acre]\n", $$fire{'100HR'});
        printf(OUT " %9.1f =    3 - 9    inch fuel [tons/acre]\n", $$fire{'1kHR'});
        printf(OUT " %9.1f =    9 - 20   inch fuel [tons/acre]\n", $$fire{'10kHR'});
        printf(OUT " %9.1f =    20+      inch fuel [tons/acre]\n", $$fire{'10k+HR'});
        printf(OUT " %9.1f = Duff Depth [inches]\n", $$fire{DUFF});
        printf(OUT " %9d = Burn-site slope [percent]\n", $$fire{SLOPE});
    }

    close(OUT);
};

return 1;
