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

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

#-------------------------------------------------------------------------------
# FileName:  sub_fuelload_NFDRS.pl
#
# Purpose:  Given lat/lon, get fuel loading from the NFDRS fuel models.
#
# by:  Susan ONeill, modified from original program by Mitchell Johnson
# created:  12/15/2003
#
#  USDA Forest Service - Pacific Northwest Wildland Fire Labratory
#  Copyright (C) 2003-2004
#-------------------------------------------------------------------------------

package NFDRS;

$USHsmoke = $ENV{USHsmoke};
$FIX_DIR = $ENV{FIXsmoke};


require "$USHsmoke/smoke_sub_fuelload_baseclass.pl";

@ISA = (Fuelload_Class);

use Math::Trig ;
use Math::Complex ;
#use NetCDF ; #eliminated in DIC 2004 (NetCDF files changed to CSV files)
use Text::CSV_XS;
use IO::File;

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

sub new
{
    ::debug(1, "using NFDRS fuelload system");
    my $self = Fuelload_Class->new;
    bless($self);
    return $self;
};


sub run_model ()
{
    $hash = shift;
    $burnID = shift;
    $ilat = shift;
    $ilon = shift;
    ::debug (100, "NFDRS:  $burnID $ilat $ilon");

    $pi = 3.14159265 ;
    $radius = 20000000/$pi ;

    # Projection Data - Lambert Azmuthal
    $olat = 45 * $pi / 180 ;   # center latitude
    $olon = -100 * $pi / 180 ; # center longitude
    $yllcorner = -2136500;     # lower left corner y coord (m)
    $xllcorner = -2050500;     # lower left corner x coord (m)
    $cellsize  = 1000;         # grid cell size (m)
    $ncols = 4587;             # number of columns
    $nrows = 2889;             # number of rows


#--------	START: PART 1 OF CHANGES TO LOAD CSV MATRIX INSTEAD OF NetCDF (DIC 2004)---------------------------------------
    #$ncfn = "nfdrfuel.cdf" ;
    #$ncid = NetCDF::open($ncfn, NetCDF::NC_NOWRITE) ;
    #$varid = NetCDF::varid($ncid, "nfdrfuel") ;

    my $ifile_NFDR = "$FIX_DIR/smoke_nfdrfuel.txt";
        die "FATAL ERROR: $! unable to open file nfdrfuel.txt\n"
        unless defined $ifile_NFDR;
    open(INFO_NFDR,$ifile_NFDR);
    @matrixNFDR = <INFO_NFDR>;

#--------	END: PART 1 OF CHANGES TO LOAD CSV MATRIX INSTEAD OF NetCDF (DIC 2004)------------------------

    $lat = $ilat * $pi / 180;
    $lon = $ilon * $pi / 180;

    # equation (24-2, 24-4, 24-5) (Snyder)
    $k = (2/(1+sin($olat)*sin($lat) + cos($olat)*cos($lat)*cos($lon-$olon)))**0.5;
    $x = $radius*$k*cos($lat)*sin($lon - $olon);
    $y = $radius*$k*(cos($olat)*sin($lat) - sin($olat)*cos($lat)*cos($lon-$olon));

    $ygrid = int(($y-$yllcorner)/$cellsize) ;
    $xgrid = int(($x-$xllcorner)/$cellsize) ;

    if ( ($ygrid < 0) || ($ygrid > $nrows) ) {
        print "Error nfdrs_fuelload.pl:  y-dim outside domain, specifying zero fuelload\n";
    }
    elsif ( ($xgrid < 0) || ($xgrid > $ncols) ) {
        print "Error nfdrs_fuelload.pl:  x-dim outside domain, specifying zero fuelload\n";
    }
    else {

# --------	START: PART 2 OF CHANGES TO LOAD CSV MATRIX INSTEAD OF NetCDF (DIC 2004)---------------------------------------
        #@coords = (0, 0, $ygrid, $xgrid) ;
        #$data = "" ;
        #NetCDF::varget1($ncid, $varid, \@coords, $data) ;
        #$fuelmodel = $data ;

        $row_NFDR=$matrixNFDR[$vegy];
        split(/ /,$row_NFDR);
        $fuelmodel=$_[$vegx];

# --------	END: PART 2 OF CHANGES TO LOAD CSV MATRIX INSTEAD OF NetCDF (DIC 2004)---------------


    }

    if ($fuelmodel < 0 || $fuelmodel > 22) {

        print "Error Running nfdrs_fuelload.pl! - NO DATA\n" ;

    } else {

       # open Fuelload_lookup.csv and get the fuel loadings
       my $ifile_handle = new IO::File "< $FIX_DIR/smoke_fuelload_lookup_nfdrs.csv";
          die "FATAL ERROR: $! unable to open file Fuelload_lookup.csv\n"
          unless defined $ifile_handle;

       my $csv = Text::CSV_XS->new();
       my $header = $csv->getline($ifile_handle);
       my $line;
       my %fuel;

       until($ifile_handle->eof()) {
          $line = $csv->getline($ifile_handle);
          @fuel{@$header} = @$line;
          if ( $fuel{FMNUM} eq $fuelmodel ) {
             ::debug (100, "NFDRS fuelloads = @$line");
             return (%fuel);
          }
       }
       ::debug (1, "Error obtaining NFDRS Fuel loadings!");
    }

    #NetCDF::close($ncid) ; #Unnecessary with changes in NetCDF to CSV files
};

sub is_valid_state ()
{
    return 1;
};
